Skip to content

Commit

Permalink
Pass contextvars for Python3.7+ only
Browse files Browse the repository at this point in the history
  • Loading branch information
RobbeSneyders committed Dec 23, 2022
1 parent 4c27584 commit 330090e
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions a2wsgi/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@

from .types import Environ, Message, Receive, Scope, Send, StartResponse, WSGIApp

try:
# Python 3.7+
import contextvars
except ImportError:
contextvars = None


class Body:
def __init__(self, loop: asyncio.AbstractEventLoop, receive: Receive) -> None:
Expand Down Expand Up @@ -184,8 +190,11 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
sender = None
try:
sender = self.loop.create_task(self.sender(send))
context = contextvars.copy_context()
func = functools.partial(context.run, self.wsgi)
if contextvars is not None:
context = contextvars.copy_context()
func = functools.partial(context.run, self.wsgi)
else:
func = self.wsgi
await self.loop.run_in_executor(
self.executor, func, environ, self.start_response
)
Expand Down

0 comments on commit 330090e

Please sign in to comment.