Closing server in Docker : gracefull shutdown #7571
-
Hello Community |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
I'm not entirely clear what you're trying to achieve. That documentation is talking about things like websockets, which are persistent connections. If you use websockets, then you'd typically store all the websockets in a set, so that during shutdown, you can ask them all to gracefully close. There are plenty of examples around, such as the chat demo: https://github.com/aio-libs/aiohttp-demos/tree/master/demos/chat/aiohttpdemo_chat Note in 3.9, we've also added a new feature that will wait a few seconds for running tasks before cancelling them. So, for regular web handlers that last no more than a few seconds, they should have the time to complete normally once using aiohttp 3.9 (which will hopefully have a beta release in the next few weeks). |
Beta Was this translation helpful? Give feedback.
I don't think that's a suggestion, that's a copy of a bit of our source code. You shouldn't use the low-level asyncio APIs for general use, and we'd likely have already cancelled the tasks before you could run that anyway.
The only way to reliably achieve it, is to track the tasks you want to keep running (similar to the the websocket example) and await them all in the shutdown handler (also, you probably want to make sure it's the first shutdown handler, to avoid DB etc. being closed before the handlers complete).