Skip to content

Commit

Permalink
Use contextvars API compatible down to python 3.7
Browse files Browse the repository at this point in the history
  • Loading branch information
jthorniley committed Sep 15, 2023
1 parent 1187cab commit 383dff3
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions asgiref/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ def __init__(self, application, scope):
self.scope = scope
self.input_queue = asyncio.Queue()
self.output_queue = asyncio.Queue()
self.future = asyncio.create_task(
# Clear context - this ensures that context vars set in the testing scope
# are not "leaked" into the application which would normally begin with
# an empty context. In Python >= 3.11 this could also be written as:
# asyncio.create_task(..., context=contextvars.Context())
self.future = contextvars.Context().run(
asyncio.create_task,
self.application(scope, self.input_queue.get, self.output_queue.put),
# Clear context - this ensure that context vars set in the testing scope
# are not "leaked" into the application which would normally begin with
# an empty context.
context=contextvars.Context(),
)

async def wait(self, timeout=1):
Expand Down

0 comments on commit 383dff3

Please sign in to comment.