August 2018
Intermediate to advanced
366 pages
10h 14m
English
The client side of this recipe is mostly taken as is from the socket serve recipe. The difference lies in the server side, which is not threaded anymore; instead, it's based on coroutines.
Given an asyncio event loop (the one we created with asyncio.new_event_loop() within the serve_for_3_seconds thread) the EchoServer.serve method creates a new coroutine-based server and tells the loop to serve requests forever until the server itself is not closed:
def serve(self, loop):
coro = asyncio.start_server(self.handle, self._host, self._port,
loop=loop)
self._server = loop.run_until_complete(coro)
print('Serving on %s:%s' % (self._host, self._port))
loop.run_until_complete(self._server.wait_closed())
print('Done')
loop.run_until_complete ...