Exemple #1
0
def test_thread():
    c = Center('127.0.0.1', 8000, start=True, block=False)
    assert c.loop.is_running()
    while not hasattr(c, 'server'):
        sleep(0.01)
    c.close()
    assert not c.loop.is_running()
def cluster(**kwargs):
    loop = asyncio.new_event_loop()
    c = Center('127.0.0.1', 8100, loop=loop)
    a = Worker('127.0.0.1', 8101, c.ip, c.port, loop=loop, **kwargs)
    b = Worker('127.0.0.1', 8102, c.ip, c.port, loop=loop, **kwargs)

    kill_q = Queue()

    @asyncio.coroutine
    def stop():
        while kill_q.empty():
            yield from asyncio.sleep(0.01, loop=loop)
        kill_q.get()

    cor = asyncio.gather(c.go(), a.go(), b.go(), loop=loop)
    cor2 = asyncio.wait([stop(), cor],
                        loop=loop,
                        return_when=asyncio.FIRST_COMPLETED)

    thread, loop = spawn_loop(cor, loop)

    try:
        yield c, a, b
    finally:
        if a.status != 'closed':
            a.close()
        if b.status != 'closed':
            b.close()
        c.close()
        kill_q.put(b'')