Define web.Application.on_startup()
signal handler
#1103
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What do these changes do?
This change introduces new signal
Application.on_startup()
that allows to register as many
on_startup
signal handlers as needed to run in the event loop along with the application's request handler just after the application's startup.This is useful to run background tasks such as listening to different event sources (message queues, etc.) to process within an application (e.g. to forward to clients connected via WebSocket).
Are there changes in behavior for the user?
The change doesn't impact on existing behavior but extends the API and the functionality.
Related issue number
#1092
Checklist
Changes done
web.Application.on_startup()
signal handler.app.on_startup()
along with the request handler within an eventloop in
web.run_app()
.Application.on_startup
signalweb.run_app()
: ensure thatApplication.startup()
is called within
web.run_app()
.Application.on_startup
signal handler:Notes
In the
tests/test_run_app.py
increased delay forloop.call_later
from0.01s
to0.02s
since with the old value loop used to stopbefore coroutines gathered for
on_startup
finished, that causedan exception.
As discussed earlier, the appropriate changes in the
aiohttp.worker.GunicornWebWorker
will be applied in a separate PR.