def test_aio_future(): loop = asyncio.get_event_loop() try: (sock1, sock2) = socket.socketpair() asyncio.Task(write(sock2)) read_fut = AioFuture(read(sock1)) read_fut.add_done_callback(lambda fut: loop.stop()) loop.call_later(6, loop.stop) loop.run_forever() assert read_fut.done() sock1.close() sock2.close() finally: loop.close()
for path in path_list: yield from conn.co_send_request('GET', path) # This generator function returns a coroutine that reads # all the responses def read_responses(): bodies = [] for path in path_list: resp = yield from conn.co_read_response() assert resp.status == 200 buf = yield from conn.co_readall() bodies.append(buf) return bodies # Create the coroutines send_crt = send_requests() recv_crt = read_responses() # Register the coroutines with the event loop send_future = AioFuture(send_crt, loop=loop) recv_future = AioFuture(recv_crt, loop=loop) # Run the event loop until the receive coroutine is done (which # implies that all the requests must have been sent as well): loop.run_until_complete(recv_future) # Get the result returned by the coroutine bodies = recv_future.result() # end-example