[docs]classAsyncioServer:"""Generic TCP server based on asyncio. Users of this class must derive from it and define the ``_handle_connection_cr`` method and coroutine. """def__init__(self):self._client_tasks=set()
[docs]asyncdefstart(self,host,port):"""Starts the server. The user must call ``stop`` to free resources properly after this method completes successfully. This method is a `coroutine`. :param host: Bind address of the server (see ``asyncio.start_server`` from the Python standard library). :param port: TCP port to bind to. """self.server=awaitasyncio.start_server(self._handle_connection,host,port,limit=4*1024*1024)
[docs]asyncdefstop(self):"""Stops the server."""wait_for=copy(self._client_tasks)fortaskinself._client_tasks:task.cancel()fortaskinwait_for:try:awaitasyncio.wait_for(task,None)exceptasyncio.CancelledError:passself.server.close()awaitself.server.wait_closed()delself.server