-
-
Notifications
You must be signed in to change notification settings - Fork 766
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5a0c292
commit 0add539
Showing
2 changed files
with
32 additions
and
7 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,25 @@ | ||
""" | ||
Module that monkey patches a2wsgi to make contextvars available to the WSGI application. | ||
This can be removed once https://github.com/abersheeran/a2wsgi/pull/31 is merged. | ||
""" | ||
import contextvars | ||
import functools | ||
|
||
import a2wsgi.wsgi | ||
from a2wsgi.wsgi import WSGIResponder | ||
|
||
|
||
class ContextWSGIResponder(WSGIResponder): | ||
"""Custom Responder that makes contextvars available to the WSGI application.""" | ||
|
||
@property | ||
def wsgi(self): | ||
context = contextvars.copy_context() | ||
return functools.partial(context.run, super().wsgi) | ||
|
||
|
||
# Monkeypatch module with our custom Responder | ||
a2wsgi.wsgi.WSGIResponder = ContextWSGIResponder | ||
|
||
# Import the WSGIMiddleware now so it uses our custom Responder | ||
from a2wsgi import WSGIMiddleware # NOQA |