How to stop a task started using asyncio.start_server #9289
-
I have a task which has a method
I create the task from main
The task is up and running. However I want to shutdown the server based on certain events once it has started. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
You can use myserver.close()
await myserver.wait_closed() Or async with myserver:
await event.wait()
# myserver will be closed when you get here |
Beta Was this translation helpful? Give feedback.
-
async def main():
await uasyncio.create_task(websocket_server(add_client)) runs to completion immediately. To give it a chance of working you need something like: async def main():
uasyncio.create_task(websocket_server(add_client))
while True:
await uasyncio.sleep(1) Have you looked at the uasyncio tutorial? |
Beta Was this translation helpful? Give feedback.
You can use
close
andwait_closed
methods:Or
async with
, and when the with finishes it'll automatically close the server. That allows you to wait on an event, eg: