Python 3.6 and 3.8.
I was using the following lines to start a server:
class MyServer:
async def main(self, handler, host, port):
self._server = await asyncio.start_server(handler, host=host, port=port)
# Next line does not work with uvloop
self._server._stop = False
where the _stop
attribute would be added to access in the handler.
Then in the handler I would set the _stop
attribute like so:
async def handler(reader, writer):
writer._transport._server._stop = True
This works beautifully when not using uvloop. But when using uvloop, this no longer works.
When I try to set (!) the _stop
attribute on the server object I immediately get this error:
AttributeError: 'uvloop.loop.Server' object has no attribute '_stop'
My question is how to "communicate" between the handler and the server ... ?
PS. When not using uvloop, this all works, because the StreamWriter._transport has an attribute _server.
Thanks to @user4815162342, I could quite easily manage via simply passing a member function as call back.
This was not my first guess, because the member function takes three arguments, while the call back function takes two (search the web for "python bound method" for more on this and why the code below still works fine).
But the code below demonstrates how it works now: