We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 9b23258 commit 278ad10Copy full SHA for 278ad10
contextvars/__init__.py
@@ -1,3 +1,4 @@
1
+import asyncio
2
import collections.abc
3
import threading
4
@@ -186,15 +187,25 @@ def copy_context():
186
187
188
189
def _get_context():
- ctx = getattr(_state, 'context', None)
190
+ state = _get_state()
191
+ ctx = getattr(state, 'context', None)
192
if ctx is None:
193
ctx = Context()
- _state.context = ctx
194
+ state.context = ctx
195
return ctx
196
197
198
def _set_context(ctx):
199
200
201
+
202
203
+def _get_state():
204
+ loop = asyncio._get_running_loop()
205
+ if loop is None:
206
+ return _state
207
+ task = asyncio.Task.current_task(loop=loop)
208
+ return _state if task is None else task
209
210
211
_state = threading.local()
0 commit comments