-
-
Notifications
You must be signed in to change notification settings - Fork 962
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move BackgroundTask execution outside of request/response cycle
- Loading branch information
Showing
6 changed files
with
188 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from typing import List, cast | ||
|
||
from starlette.background import BackgroundTask | ||
from starlette.types import ASGIApp, Receive, Scope, Send | ||
|
||
# consider this a private implementation detail subject to change | ||
# do not rely on this key | ||
_SCOPE_KEY = "starlette._background" | ||
|
||
|
||
_BackgroundTaskList = List[BackgroundTask] | ||
|
||
|
||
def is_background_task_middleware_installed(scope: Scope) -> bool: | ||
return _SCOPE_KEY in scope | ||
|
||
|
||
def add_tasks(scope: Scope, __task: BackgroundTask) -> None: | ||
if _SCOPE_KEY not in scope: # pragma: no cover | ||
raise RuntimeError( | ||
"`add_tasks` can only be used if `BackgroundTaskMIddleware is installed" | ||
) | ||
cast(_BackgroundTaskList, scope[_SCOPE_KEY]).append(__task) | ||
|
||
|
||
class BackgroundTaskMiddleware: | ||
def __init__(self, app: ASGIApp) -> None: | ||
self._app = app | ||
|
||
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: | ||
tasks: _BackgroundTaskList | ||
scope[_SCOPE_KEY] = tasks = [] | ||
try: | ||
await self._app(scope, receive, send) | ||
finally: | ||
for task in tasks: | ||
await task() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters