asgiref 3.9.2__py3-none-any.whl → 3.11.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- asgiref/__init__.py +1 -1
- asgiref/sync.py +75 -4
- {asgiref-3.9.2.dist-info → asgiref-3.11.0.dist-info}/METADATA +1 -1
- {asgiref-3.9.2.dist-info → asgiref-3.11.0.dist-info}/RECORD +7 -7
- {asgiref-3.9.2.dist-info → asgiref-3.11.0.dist-info}/WHEEL +0 -0
- {asgiref-3.9.2.dist-info → asgiref-3.11.0.dist-info}/licenses/LICENSE +0 -0
- {asgiref-3.9.2.dist-info → asgiref-3.11.0.dist-info}/top_level.txt +0 -0
asgiref/__init__.py
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
__version__ = "3.
|
|
1
|
+
__version__ = "3.11.0"
|
asgiref/sync.py
CHANGED
|
@@ -69,6 +69,45 @@ else:
|
|
|
69
69
|
return func
|
|
70
70
|
|
|
71
71
|
|
|
72
|
+
class AsyncSingleThreadContext:
|
|
73
|
+
"""Context manager to run async code inside the same thread.
|
|
74
|
+
|
|
75
|
+
Normally, AsyncToSync functions run either inside a separate ThreadPoolExecutor or
|
|
76
|
+
the main event loop if it exists. This context manager ensures that all AsyncToSync
|
|
77
|
+
functions execute within the same thread.
|
|
78
|
+
|
|
79
|
+
This context manager is re-entrant, so only the outer-most call to
|
|
80
|
+
AsyncSingleThreadContext will set the context.
|
|
81
|
+
|
|
82
|
+
Usage:
|
|
83
|
+
|
|
84
|
+
>>> import asyncio
|
|
85
|
+
>>> with AsyncSingleThreadContext():
|
|
86
|
+
... async_to_sync(asyncio.sleep(1))()
|
|
87
|
+
"""
|
|
88
|
+
|
|
89
|
+
def __init__(self):
|
|
90
|
+
self.token = None
|
|
91
|
+
|
|
92
|
+
def __enter__(self):
|
|
93
|
+
try:
|
|
94
|
+
AsyncToSync.async_single_thread_context.get()
|
|
95
|
+
except LookupError:
|
|
96
|
+
self.token = AsyncToSync.async_single_thread_context.set(self)
|
|
97
|
+
|
|
98
|
+
return self
|
|
99
|
+
|
|
100
|
+
def __exit__(self, exc, value, tb):
|
|
101
|
+
if not self.token:
|
|
102
|
+
return
|
|
103
|
+
|
|
104
|
+
executor = AsyncToSync.context_to_thread_executor.pop(self, None)
|
|
105
|
+
if executor:
|
|
106
|
+
executor.shutdown()
|
|
107
|
+
|
|
108
|
+
AsyncToSync.async_single_thread_context.reset(self.token)
|
|
109
|
+
|
|
110
|
+
|
|
72
111
|
class ThreadSensitiveContext:
|
|
73
112
|
"""Async context manager to manage context for thread sensitive mode
|
|
74
113
|
|
|
@@ -131,6 +170,14 @@ class AsyncToSync(Generic[_P, _R]):
|
|
|
131
170
|
# inside create_task, we'll look it up here from the running event loop.
|
|
132
171
|
loop_thread_executors: "Dict[asyncio.AbstractEventLoop, CurrentThreadExecutor]" = {}
|
|
133
172
|
|
|
173
|
+
async_single_thread_context: "contextvars.ContextVar[AsyncSingleThreadContext]" = (
|
|
174
|
+
contextvars.ContextVar("async_single_thread_context")
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
context_to_thread_executor: "weakref.WeakKeyDictionary[AsyncSingleThreadContext, ThreadPoolExecutor]" = (
|
|
178
|
+
weakref.WeakKeyDictionary()
|
|
179
|
+
)
|
|
180
|
+
|
|
134
181
|
def __init__(
|
|
135
182
|
self,
|
|
136
183
|
awaitable: Union[
|
|
@@ -246,8 +293,24 @@ class AsyncToSync(Generic[_P, _R]):
|
|
|
246
293
|
running_in_main_event_loop = False
|
|
247
294
|
|
|
248
295
|
if not running_in_main_event_loop:
|
|
249
|
-
|
|
250
|
-
|
|
296
|
+
loop_executor = None
|
|
297
|
+
|
|
298
|
+
if self.async_single_thread_context.get(None):
|
|
299
|
+
single_thread_context = self.async_single_thread_context.get()
|
|
300
|
+
|
|
301
|
+
if single_thread_context in self.context_to_thread_executor:
|
|
302
|
+
loop_executor = self.context_to_thread_executor[
|
|
303
|
+
single_thread_context
|
|
304
|
+
]
|
|
305
|
+
else:
|
|
306
|
+
loop_executor = ThreadPoolExecutor(max_workers=1)
|
|
307
|
+
self.context_to_thread_executor[
|
|
308
|
+
single_thread_context
|
|
309
|
+
] = loop_executor
|
|
310
|
+
else:
|
|
311
|
+
# Make our own event loop - in a new thread - and run inside that.
|
|
312
|
+
loop_executor = ThreadPoolExecutor(max_workers=1)
|
|
313
|
+
|
|
251
314
|
loop_future = loop_executor.submit(asyncio.run, new_loop_wrap())
|
|
252
315
|
# Run the CurrentThreadExecutor until the future is done.
|
|
253
316
|
current_executor.run_until_future(loop_future)
|
|
@@ -361,6 +424,7 @@ class SyncToAsync(Generic[_P, _R]):
|
|
|
361
424
|
func: Callable[_P, _R],
|
|
362
425
|
thread_sensitive: bool = True,
|
|
363
426
|
executor: Optional["ThreadPoolExecutor"] = None,
|
|
427
|
+
context: Optional[contextvars.Context] = None,
|
|
364
428
|
) -> None:
|
|
365
429
|
if (
|
|
366
430
|
not callable(func)
|
|
@@ -369,6 +433,7 @@ class SyncToAsync(Generic[_P, _R]):
|
|
|
369
433
|
):
|
|
370
434
|
raise TypeError("sync_to_async can only be applied to sync functions.")
|
|
371
435
|
self.func = func
|
|
436
|
+
self.context = context
|
|
372
437
|
functools.update_wrapper(self, func)
|
|
373
438
|
self._thread_sensitive = thread_sensitive
|
|
374
439
|
markcoroutinefunction(self)
|
|
@@ -417,7 +482,7 @@ class SyncToAsync(Generic[_P, _R]):
|
|
|
417
482
|
# Use the passed in executor, or the loop's default if it is None
|
|
418
483
|
executor = self._executor
|
|
419
484
|
|
|
420
|
-
context = contextvars.copy_context()
|
|
485
|
+
context = contextvars.copy_context() if self.context is None else self.context
|
|
421
486
|
child = functools.partial(self.func, *args, **kwargs)
|
|
422
487
|
func = context.run
|
|
423
488
|
task_context: List[asyncio.Task[Any]] = []
|
|
@@ -455,7 +520,8 @@ class SyncToAsync(Generic[_P, _R]):
|
|
|
455
520
|
exec_coro.cancel()
|
|
456
521
|
ret = await exec_coro
|
|
457
522
|
finally:
|
|
458
|
-
|
|
523
|
+
if self.context is None:
|
|
524
|
+
_restore_context(context)
|
|
459
525
|
self.deadlock_context.set(False)
|
|
460
526
|
|
|
461
527
|
return ret
|
|
@@ -548,6 +614,7 @@ def sync_to_async(
|
|
|
548
614
|
*,
|
|
549
615
|
thread_sensitive: bool = True,
|
|
550
616
|
executor: Optional["ThreadPoolExecutor"] = None,
|
|
617
|
+
context: Optional[contextvars.Context] = None,
|
|
551
618
|
) -> Callable[[Callable[_P, _R]], Callable[_P, Coroutine[Any, Any, _R]]]:
|
|
552
619
|
...
|
|
553
620
|
|
|
@@ -558,6 +625,7 @@ def sync_to_async(
|
|
|
558
625
|
*,
|
|
559
626
|
thread_sensitive: bool = True,
|
|
560
627
|
executor: Optional["ThreadPoolExecutor"] = None,
|
|
628
|
+
context: Optional[contextvars.Context] = None,
|
|
561
629
|
) -> Callable[_P, Coroutine[Any, Any, _R]]:
|
|
562
630
|
...
|
|
563
631
|
|
|
@@ -567,6 +635,7 @@ def sync_to_async(
|
|
|
567
635
|
*,
|
|
568
636
|
thread_sensitive: bool = True,
|
|
569
637
|
executor: Optional["ThreadPoolExecutor"] = None,
|
|
638
|
+
context: Optional[contextvars.Context] = None,
|
|
570
639
|
) -> Union[
|
|
571
640
|
Callable[[Callable[_P, _R]], Callable[_P, Coroutine[Any, Any, _R]]],
|
|
572
641
|
Callable[_P, Coroutine[Any, Any, _R]],
|
|
@@ -576,9 +645,11 @@ def sync_to_async(
|
|
|
576
645
|
f,
|
|
577
646
|
thread_sensitive=thread_sensitive,
|
|
578
647
|
executor=executor,
|
|
648
|
+
context=context,
|
|
579
649
|
)
|
|
580
650
|
return SyncToAsync(
|
|
581
651
|
func,
|
|
582
652
|
thread_sensitive=thread_sensitive,
|
|
583
653
|
executor=executor,
|
|
654
|
+
context=context,
|
|
584
655
|
)
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
asgiref/__init__.py,sha256=
|
|
1
|
+
asgiref/__init__.py,sha256=cpptu6yAKjujkKCECVRXYkw3SmUpyBiTvJvB3Mmcu5s,23
|
|
2
2
|
asgiref/compatibility.py,sha256=DhY1SOpOvOw0Y1lSEjCqg-znRUQKecG3LTaV48MZi68,1606
|
|
3
3
|
asgiref/current_thread_executor.py,sha256=42CU1VODLTk-_PYise-cP1XgyAvI5Djc8f97owFzdrs,4157
|
|
4
4
|
asgiref/local.py,sha256=ZZeWWIXptVU4GbNApMMWQ-skuglvodcQA5WpzJDMxh4,4912
|
|
5
5
|
asgiref/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
6
6
|
asgiref/server.py,sha256=3A68169Nuh2sTY_2O5JzRd_opKObWvvrEFcrXssq3kA,6311
|
|
7
|
-
asgiref/sync.py,sha256=
|
|
7
|
+
asgiref/sync.py,sha256=kYfWtf4CI438zIw85kX9YQx0y64B1N6AM0PcRGf01M4,22927
|
|
8
8
|
asgiref/testing.py,sha256=U5wcs_-ZYTO5SIGfl80EqRAGv_T8BHrAhvAKRuuztT4,4421
|
|
9
9
|
asgiref/timeout.py,sha256=LtGL-xQpG8JHprdsEUCMErJ0kNWj4qwWZhEHJ3iKu4s,3627
|
|
10
10
|
asgiref/typing.py,sha256=Zi72AZlOyF1C7N14LLZnpAdfUH4ljoBqFdQo_bBKMq0,6290
|
|
11
11
|
asgiref/wsgi.py,sha256=J8OAgirfsYHZmxxqIGfFiZ43uq1qKKv2xGMkRISNIo4,6742
|
|
12
|
-
asgiref-3.
|
|
13
|
-
asgiref-3.
|
|
14
|
-
asgiref-3.
|
|
15
|
-
asgiref-3.
|
|
16
|
-
asgiref-3.
|
|
12
|
+
asgiref-3.11.0.dist-info/licenses/LICENSE,sha256=uEZBXRtRTpwd_xSiLeuQbXlLxUbKYSn5UKGM0JHipmk,1552
|
|
13
|
+
asgiref-3.11.0.dist-info/METADATA,sha256=gV4IrytPfzCXgnDhv_rM5GBoUUd_Tz-QvqZSFKEmVGs,9287
|
|
14
|
+
asgiref-3.11.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
15
|
+
asgiref-3.11.0.dist-info/top_level.txt,sha256=bokQjCzwwERhdBiPdvYEZa4cHxT4NCeAffQNUqJ8ssg,8
|
|
16
|
+
asgiref-3.11.0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|