-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to gracefully stop server programatically #2950
Comments
FYI, raising |
FYI |
I'm running an aiohttp server in its own thread for a test. I'd like to end the test cleanly so called await app.shutdown()
await app.cleanup() but the server kept going. :-( |
@TomGoBravo FYI you may experience intermittent problems when running asyncio loop in non-main threads. |
@TomGoBravo Here is the code import asyncio
from aiohttp import web
app = web.Application()
routes = web.RouteTableDef()
@routes.get("/hello")
async def hello(request):
return web.Response(text="Hello World")
async def bootup(this_app):
asyncio.create_task(background())
async def background():
await asyncio.sleep(5)
print("Start shutting down")
await app.shutdown()
print("Start cleaning up")
await app.cleanup()
if __name__ == "__main__":
app.add_routes(routes)
app.on_startup.append(bootup)
web.run_app(app, host="0.0.0.0", port=80)
print("Finished") output ======== Running on http://0.0.0.0:80 ========
(Press CTRL+C to quit)
Start shutting down
Start cleaning up
|
I solved this by using the async def process():
app = web.Application()
runner = web.AppRunner(app)
loop = asyncio.get_running_loop()
running = loop.create_future()
async def stop(e):
await runner.cleanup()
running.set_exception(e)
... # app setup
await runner.setup()
site = web.TCPSite(runner, *address)
await site.start()
await running
def main(config=None):
asyncio.run(process()) Would be nice to have a |
I kindly ask for this to be reopened. It is very frustrating that there only seem to be two ways of causing a graceful shutdown of an aiohttp application as discussed in this issue:
Even then, it seems questionable just how graceful a shutdown by throwing a |
In my case the easiest way to do this in most cases is to send a signal like |
This did the trick for me and also exits gracefully. import signal
def shutdown():
signal.raise_signal(signal.SIGTERM) Call |
Long story short
I have a aiohtto-based service which has the main purpose to just perform a task running in the background. The http is here just to respond with health status of the background task. I don't see any way to programatically stop aiohttp server in case of exception raised by the task. The whole service should stop, because it doesn't make sense without healthy task. I tried something like this (the example is based on the aiohttp docs):
Expected behaviour
I'd expect aiohttp to run all the
on_cleanup
hooks. Or some documentation on how to programatically shutdown the service.Actual behaviour
on_cleanup
hooks are not called.Steps to reproduce
Your environment
The text was updated successfully, but these errors were encountered: