Skip to content

Commit

Permalink
Preserve execution context when running code in ThreadPool (#31)
Browse files Browse the repository at this point in the history
* Pass contextvars to threadpool executor

* Remove 3.6 fallback
  • Loading branch information
RobbeSneyders committed Dec 26, 2022
1 parent f6d86c7 commit cecacbd
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion a2wsgi/wsgi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import asyncio
import collections
import contextvars
import functools
import os
import sys
import typing
Expand Down Expand Up @@ -184,8 +186,10 @@ 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)
await self.loop.run_in_executor(
self.executor, self.wsgi, environ, self.start_response
self.executor, func, environ, self.start_response
)
self.send_queue.append(None)
self.send_event.set()
Expand Down

0 comments on commit cecacbd

Please sign in to comment.