sync.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. import asyncio
  2. import asyncio.coroutines
  3. import contextvars
  4. import functools
  5. import inspect
  6. import os
  7. import sys
  8. import threading
  9. import warnings
  10. import weakref
  11. from collections.abc import Awaitable, Callable, Coroutine
  12. from concurrent.futures import Future, InvalidStateError, ThreadPoolExecutor
  13. from typing import (
  14. TYPE_CHECKING,
  15. Any,
  16. Dict,
  17. Generic,
  18. List,
  19. Optional,
  20. ParamSpec,
  21. TypeVar,
  22. overload,
  23. )
  24. from .current_thread_executor import CurrentThreadExecutor
  25. from .local import Local, _rehome, _Storage
  26. if TYPE_CHECKING:
  27. # This is not available to import at runtime
  28. from _typeshed import OptExcInfo
  29. _F = TypeVar("_F", bound=Callable[..., Any])
  30. _P = ParamSpec("_P")
  31. _R = TypeVar("_R")
  32. def _restore_context(context: contextvars.Context) -> None:
  33. # Check for changes in contextvars, and set them to the current
  34. # context for downstream consumers
  35. for cvar in context:
  36. cvalue = context.get(cvar)
  37. # asgiref is deliberately moving this context onto the current thread,
  38. # so re-home any Local storage to it. This keeps Local data visible
  39. # across async_to_sync / sync_to_async boundaries while leaving data
  40. # merely inherited by an unrelated thread isolated (see asgiref.local).
  41. if isinstance(cvalue, _Storage):
  42. cvalue = _rehome(cvalue)
  43. try:
  44. if cvar.get() != cvalue:
  45. cvar.set(cvalue)
  46. except LookupError:
  47. cvar.set(cvalue)
  48. # Python 3.12 deprecates asyncio.iscoroutinefunction() as an alias for
  49. # inspect.iscoroutinefunction(), whilst also removing the _is_coroutine marker.
  50. # The latter is replaced with the inspect.markcoroutinefunction decorator.
  51. # Until 3.12 is the minimum supported Python version, provide a shim.
  52. if hasattr(inspect, "markcoroutinefunction"):
  53. iscoroutinefunction = inspect.iscoroutinefunction
  54. markcoroutinefunction: Callable[[_F], _F] = inspect.markcoroutinefunction
  55. else:
  56. iscoroutinefunction = asyncio.iscoroutinefunction # type: ignore[assignment]
  57. def markcoroutinefunction(func: _F) -> _F:
  58. func._is_coroutine = asyncio.coroutines._is_coroutine # type: ignore
  59. return func
  60. class AsyncSingleThreadContext:
  61. """Context manager to run async code inside the same thread.
  62. Normally, AsyncToSync functions run either inside a separate ThreadPoolExecutor or
  63. the main event loop if it exists. This context manager ensures that all AsyncToSync
  64. functions execute within the same thread.
  65. This context manager is re-entrant, so only the outer-most call to
  66. AsyncSingleThreadContext will set the context.
  67. Usage:
  68. >>> import asyncio
  69. >>> with AsyncSingleThreadContext():
  70. ... async_to_sync(asyncio.sleep(1))()
  71. """
  72. def __init__(self):
  73. self.token = None
  74. def __enter__(self):
  75. try:
  76. AsyncToSync.async_single_thread_context.get()
  77. except LookupError:
  78. self.token = AsyncToSync.async_single_thread_context.set(self)
  79. return self
  80. def __exit__(self, exc, value, tb):
  81. if not self.token:
  82. return
  83. executor = AsyncToSync.context_to_thread_executor.pop(self, None)
  84. if executor:
  85. executor.shutdown()
  86. AsyncToSync.async_single_thread_context.reset(self.token)
  87. class ThreadSensitiveContext:
  88. """Async context manager to manage context for thread sensitive mode
  89. This context manager controls which thread pool executor is used when in
  90. thread sensitive mode. By default, a single thread pool executor is shared
  91. within a process.
  92. The ThreadSensitiveContext() context manager may be used to specify a
  93. thread pool per context.
  94. This context manager is re-entrant, so only the outer-most call to
  95. ThreadSensitiveContext will set the context.
  96. Usage:
  97. >>> import time
  98. >>> async with ThreadSensitiveContext():
  99. ... await sync_to_async(time.sleep, 1)()
  100. """
  101. def __init__(self):
  102. self.token = None
  103. async def __aenter__(self):
  104. try:
  105. SyncToAsync.thread_sensitive_context.get()
  106. except LookupError:
  107. self.token = SyncToAsync.thread_sensitive_context.set(self)
  108. return self
  109. async def __aexit__(self, exc, value, tb):
  110. if not self.token:
  111. return
  112. executor = SyncToAsync.context_to_thread_executor.pop(self, None)
  113. SyncToAsync.thread_sensitive_context.reset(self.token)
  114. if executor:
  115. # The executor's worker thread may itself be waiting for this
  116. # event loop, so a blocking shutdown() here would deadlock it.
  117. # Join in a dedicated thread, not the loop's default executor:
  118. # work queued there may itself be needed to unpark the worker,
  119. # and joins occupying its slots would starve it.
  120. future: "Future[None]" = Future()
  121. def join() -> None:
  122. executor.shutdown()
  123. try:
  124. future.set_result(None)
  125. except InvalidStateError:
  126. # The await below was cancelled while we were joining.
  127. pass
  128. threading.Thread(target=join, daemon=True).start()
  129. await asyncio.wrap_future(future)
  130. class AsyncToSync(Generic[_P, _R]):
  131. """
  132. Utility class which turns an awaitable that only works on the thread with
  133. the event loop into a synchronous callable that works in a subthread.
  134. If the call stack contains an async loop, the code runs there.
  135. Otherwise, the code runs in a new loop in a new thread.
  136. Either way, this thread then pauses and waits to run any thread_sensitive
  137. code called from further down the call stack using SyncToAsync, before
  138. finally exiting once the async task returns.
  139. """
  140. # Keeps a reference to the CurrentThreadExecutor in local context, so that
  141. # any sync_to_async inside the wrapped code can find it.
  142. executors: "Local" = Local()
  143. # When we can't find a CurrentThreadExecutor from the context, such as
  144. # inside create_task, we'll look it up here from the running event loop.
  145. loop_thread_executors: "Dict[asyncio.AbstractEventLoop, CurrentThreadExecutor]" = {}
  146. async_single_thread_context: "contextvars.ContextVar[AsyncSingleThreadContext]" = (
  147. contextvars.ContextVar("async_single_thread_context")
  148. )
  149. context_to_thread_executor: (
  150. "weakref.WeakKeyDictionary[AsyncSingleThreadContext, ThreadPoolExecutor]"
  151. ) = weakref.WeakKeyDictionary()
  152. def __init__(
  153. self,
  154. awaitable: Callable[_P, Coroutine[Any, Any, _R]] | Callable[_P, Awaitable[_R]],
  155. force_new_loop: bool = False,
  156. ):
  157. if not callable(awaitable) or (
  158. not iscoroutinefunction(awaitable)
  159. and not iscoroutinefunction(getattr(awaitable, "__call__", awaitable))
  160. ):
  161. # Python does not have very reliable detection of async functions
  162. # (lots of false negatives) so this is just a warning.
  163. warnings.warn(
  164. "async_to_sync was passed a non-async-marked callable", stacklevel=2
  165. )
  166. self.awaitable = awaitable
  167. try:
  168. self.__self__ = self.awaitable.__self__ # type: ignore[union-attr]
  169. except AttributeError:
  170. pass
  171. self.force_new_loop = force_new_loop
  172. def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _R:
  173. __traceback_hide__ = True # noqa: F841
  174. main_event_loop = None
  175. if not self.force_new_loop:
  176. # There's no event loop in this thread. Look for the threadlocal if
  177. # we're inside SyncToAsync
  178. main_event_loop_pid = getattr(
  179. SyncToAsync.threadlocal, "main_event_loop_pid", None
  180. )
  181. # We make sure the parent loop is from the same process - if
  182. # they've forked, this is not going to be valid any more (#194)
  183. if main_event_loop_pid and main_event_loop_pid == os.getpid():
  184. main_event_loop = getattr(
  185. SyncToAsync.threadlocal, "main_event_loop", None
  186. )
  187. # You can't call AsyncToSync from a thread with a running event loop
  188. try:
  189. asyncio.get_running_loop()
  190. except RuntimeError:
  191. pass
  192. else:
  193. raise RuntimeError(
  194. "You cannot use AsyncToSync in the same thread as an async event loop - "
  195. "just await the async function directly."
  196. )
  197. # Make a future for the return information
  198. call_result: "Future[_R]" = Future()
  199. # Make a CurrentThreadExecutor we'll use to idle in this thread - we
  200. # need one for every sync frame, even if there's one above us in the
  201. # same thread.
  202. old_executor = getattr(self.executors, "current", None)
  203. current_executor = CurrentThreadExecutor(old_executor)
  204. self.executors.current = current_executor
  205. # Wrapping context in list so it can be reassigned from within
  206. # `main_wrap`.
  207. context = [contextvars.copy_context()]
  208. # Get task context so that parent task knows which task to propagate
  209. # an asyncio.CancelledError to.
  210. task_context = getattr(SyncToAsync.threadlocal, "task_context", None)
  211. # Use call_soon_threadsafe to schedule a synchronous callback on the
  212. # main event loop's thread if it's there, otherwise make a new loop
  213. # in this thread.
  214. try:
  215. awaitable = self.main_wrap(
  216. call_result,
  217. sys.exc_info(),
  218. task_context,
  219. context,
  220. # prepare an awaitable which can be passed as is to self.main_wrap,
  221. # so that `args` and `kwargs` don't need to be
  222. # destructured when passed to self.main_wrap
  223. # (which is required by `ParamSpec`)
  224. # as that may cause overlapping arguments
  225. self.awaitable(*args, **kwargs),
  226. )
  227. async def new_loop_wrap() -> None:
  228. loop = asyncio.get_running_loop()
  229. self.loop_thread_executors[loop] = current_executor
  230. try:
  231. await awaitable
  232. finally:
  233. del self.loop_thread_executors[loop]
  234. if main_event_loop is not None:
  235. try:
  236. main_event_loop.call_soon_threadsafe(
  237. main_event_loop.create_task, awaitable
  238. )
  239. except RuntimeError:
  240. running_in_main_event_loop = False
  241. else:
  242. running_in_main_event_loop = True
  243. # Run the CurrentThreadExecutor until the future is done.
  244. current_executor.run_until_future(call_result)
  245. else:
  246. running_in_main_event_loop = False
  247. if not running_in_main_event_loop:
  248. loop_executor = None
  249. if self.async_single_thread_context.get(None):
  250. single_thread_context = self.async_single_thread_context.get()
  251. if single_thread_context in self.context_to_thread_executor:
  252. loop_executor = self.context_to_thread_executor[
  253. single_thread_context
  254. ]
  255. else:
  256. loop_executor = ThreadPoolExecutor(max_workers=1)
  257. self.context_to_thread_executor[single_thread_context] = (
  258. loop_executor
  259. )
  260. else:
  261. # Make our own event loop - in a new thread - and run inside that.
  262. loop_executor = ThreadPoolExecutor(max_workers=1)
  263. loop_future = loop_executor.submit(asyncio.run, new_loop_wrap())
  264. # Run the CurrentThreadExecutor until the future is done.
  265. current_executor.run_until_future(loop_future)
  266. # Wait for future and/or allow for exception propagation
  267. loop_future.result()
  268. finally:
  269. _restore_context(context[0])
  270. # Restore old current thread executor state
  271. self.executors.current = old_executor
  272. # Wait for results from the future.
  273. return call_result.result()
  274. def __get__(self, parent: Any, objtype: Any) -> Callable[_P, _R]:
  275. """
  276. Include self for methods
  277. """
  278. func = functools.partial(self.__call__, parent)
  279. return functools.update_wrapper(func, self.awaitable)
  280. async def main_wrap(
  281. self,
  282. call_result: "Future[_R]",
  283. exc_info: "OptExcInfo",
  284. task_context: "Optional[List[asyncio.Task[Any]]]",
  285. context: list[contextvars.Context],
  286. awaitable: Coroutine[Any, Any, _R] | Awaitable[_R],
  287. ) -> None:
  288. """
  289. Wraps the awaitable with something that puts the result into the
  290. result/exception future.
  291. """
  292. __traceback_hide__ = True # noqa: F841
  293. if context is not None:
  294. _restore_context(context[0])
  295. current_task = asyncio.current_task()
  296. if current_task is not None and task_context is not None:
  297. task_context.append(current_task)
  298. try:
  299. # If we have an exception, run the function inside the except block
  300. # after raising it so exc_info is correctly populated.
  301. if exc_info[1]:
  302. try:
  303. raise exc_info[1]
  304. except BaseException:
  305. result = await awaitable
  306. else:
  307. result = await awaitable
  308. except BaseException as e:
  309. call_result.set_exception(e)
  310. else:
  311. call_result.set_result(result)
  312. finally:
  313. if current_task is not None and task_context is not None:
  314. task_context.remove(current_task)
  315. context[0] = contextvars.copy_context()
  316. class SyncToAsync(Generic[_P, _R]):
  317. """
  318. Utility class which turns a synchronous callable into an awaitable that
  319. runs in a threadpool. It also sets a threadlocal inside the thread so
  320. calls to AsyncToSync can escape it.
  321. If thread_sensitive is passed, the code will run in the same thread as any
  322. outer code. This is needed for underlying Python code that is not
  323. threadsafe (for example, code which handles SQLite database connections).
  324. If the outermost program is async (i.e. SyncToAsync is outermost), then
  325. this will be a dedicated single sub-thread that all sync code runs in,
  326. one after the other. If the outermost program is sync (i.e. AsyncToSync is
  327. outermost), this will just be the main thread. This is achieved by idling
  328. with a CurrentThreadExecutor while AsyncToSync is blocking its sync parent,
  329. rather than just blocking.
  330. If executor is passed in, that will be used instead of the loop's default executor.
  331. In order to pass in an executor, thread_sensitive must be set to False, otherwise
  332. a TypeError will be raised.
  333. """
  334. # Storage for main event loop references
  335. threadlocal = threading.local()
  336. # Single-thread executor for thread-sensitive code
  337. single_thread_executor = ThreadPoolExecutor(max_workers=1)
  338. # Maintain a contextvar for the current execution context. Optionally used
  339. # for thread sensitive mode.
  340. thread_sensitive_context: "contextvars.ContextVar[ThreadSensitiveContext]" = (
  341. contextvars.ContextVar("thread_sensitive_context")
  342. )
  343. # Contextvar that is used to detect if the single thread executor
  344. # would be awaited on while already being used in the same context
  345. deadlock_context: "contextvars.ContextVar[bool]" = contextvars.ContextVar(
  346. "deadlock_context"
  347. )
  348. # Maintaining a weak reference to the context ensures that thread pools are
  349. # erased once the context goes out of scope. This terminates the thread pool.
  350. context_to_thread_executor: (
  351. "weakref.WeakKeyDictionary[ThreadSensitiveContext, ThreadPoolExecutor]"
  352. ) = weakref.WeakKeyDictionary()
  353. def __init__(
  354. self,
  355. func: Callable[_P, _R],
  356. thread_sensitive: bool = True,
  357. executor: Optional["ThreadPoolExecutor"] = None,
  358. context: contextvars.Context | None = None,
  359. ) -> None:
  360. if (
  361. not callable(func)
  362. or iscoroutinefunction(func)
  363. or iscoroutinefunction(getattr(func, "__call__", func))
  364. ):
  365. raise TypeError("sync_to_async can only be applied to sync functions.")
  366. functools.update_wrapper(self, func)
  367. self.func = func
  368. self.context = context
  369. self._thread_sensitive = thread_sensitive
  370. markcoroutinefunction(self)
  371. if thread_sensitive and executor is not None:
  372. raise TypeError("executor must not be set when thread_sensitive is True")
  373. self._executor = executor
  374. try:
  375. self.__self__ = func.__self__ # type: ignore
  376. except AttributeError:
  377. pass
  378. async def __call__(self, *args: _P.args, **kwargs: _P.kwargs) -> _R:
  379. __traceback_hide__ = True # noqa: F841
  380. loop = asyncio.get_running_loop()
  381. # Work out what thread to run the code in
  382. if self._thread_sensitive:
  383. current_thread_executor = getattr(AsyncToSync.executors, "current", None)
  384. if current_thread_executor:
  385. # If we have a parent sync thread above somewhere, use that
  386. executor = current_thread_executor
  387. elif self.thread_sensitive_context.get(None):
  388. # If we have a way of retrieving the current context, attempt
  389. # to use a per-context thread pool executor
  390. thread_sensitive_context = self.thread_sensitive_context.get()
  391. if thread_sensitive_context in self.context_to_thread_executor:
  392. # Re-use thread executor in current context
  393. executor = self.context_to_thread_executor[thread_sensitive_context]
  394. else:
  395. # Create new thread executor in current context
  396. executor = ThreadPoolExecutor(max_workers=1)
  397. self.context_to_thread_executor[thread_sensitive_context] = executor
  398. elif loop in AsyncToSync.loop_thread_executors:
  399. # Re-use thread executor for running loop
  400. executor = AsyncToSync.loop_thread_executors[loop]
  401. elif self.deadlock_context.get(False):
  402. raise RuntimeError(
  403. "Single thread executor already being used, would deadlock"
  404. )
  405. else:
  406. # Otherwise, we run it in a fixed single thread
  407. executor = self.single_thread_executor
  408. self.deadlock_context.set(True)
  409. else:
  410. # Use the passed in executor, or the loop's default if it is None
  411. executor = self._executor
  412. context = contextvars.copy_context() if self.context is None else self.context
  413. # ``child`` is the deferred sync function to be run, with its args
  414. # and kwargs bound.
  415. child = functools.partial(self.func, *args, **kwargs)
  416. # On the worker thread, thread_handler runs ``func(child)``. ``func``
  417. # enters ``context`` (via context.run); then, inside it, ``run_child``
  418. # re-homes any Local storage to the worker thread so it stays visible
  419. # there (see _restore_context), and finally calls ``child``.
  420. def func(child: Callable[[], _R]) -> _R:
  421. def run_child() -> _R:
  422. _restore_context(context)
  423. return child()
  424. return context.run(run_child)
  425. task_context: list[asyncio.Task[Any]] = []
  426. # Run the code in the right thread
  427. exec_coro = loop.run_in_executor(
  428. executor,
  429. functools.partial(
  430. self.thread_handler,
  431. loop,
  432. sys.exc_info(),
  433. task_context,
  434. func,
  435. child,
  436. ),
  437. )
  438. ret: _R
  439. try:
  440. ret = await asyncio.shield(exec_coro)
  441. except asyncio.CancelledError:
  442. cancel_parent = True
  443. try:
  444. task = task_context[0]
  445. task.cancel()
  446. try:
  447. await task
  448. cancel_parent = False
  449. except asyncio.CancelledError:
  450. pass
  451. except IndexError:
  452. pass
  453. if exec_coro.done():
  454. raise
  455. if cancel_parent:
  456. exec_coro.cancel()
  457. ret = await exec_coro
  458. finally:
  459. if self.context is None:
  460. _restore_context(context)
  461. self.deadlock_context.set(False)
  462. return ret
  463. def __get__(
  464. self, parent: Any, objtype: Any
  465. ) -> Callable[_P, Coroutine[Any, Any, _R]]:
  466. """
  467. Include self for methods
  468. """
  469. func = functools.partial(self.__call__, parent)
  470. return functools.update_wrapper(func, self.func)
  471. def thread_handler(self, loop, exc_info, task_context, func, *args, **kwargs):
  472. """
  473. Wraps the sync application with exception handling.
  474. """
  475. __traceback_hide__ = True # noqa: F841
  476. # Set the threadlocal for AsyncToSync
  477. self.threadlocal.main_event_loop = loop
  478. self.threadlocal.main_event_loop_pid = os.getpid()
  479. self.threadlocal.task_context = task_context
  480. # Run the function
  481. # If we have an exception, run the function inside the except block
  482. # after raising it so exc_info is correctly populated.
  483. if exc_info[1]:
  484. try:
  485. raise exc_info[1]
  486. except BaseException:
  487. return func(*args, **kwargs)
  488. else:
  489. return func(*args, **kwargs)
  490. @overload
  491. def async_to_sync(
  492. *,
  493. force_new_loop: bool = False,
  494. ) -> Callable[
  495. [Callable[_P, Coroutine[Any, Any, _R]] | Callable[_P, Awaitable[_R]]],
  496. Callable[_P, _R],
  497. ]: ...
  498. @overload
  499. def async_to_sync(
  500. awaitable: Callable[_P, Coroutine[Any, Any, _R]] | Callable[_P, Awaitable[_R]],
  501. *,
  502. force_new_loop: bool = False,
  503. ) -> Callable[_P, _R]: ...
  504. def async_to_sync(
  505. awaitable: None | (
  506. Callable[_P, Coroutine[Any, Any, _R]] | Callable[_P, Awaitable[_R]]
  507. ) = None,
  508. *,
  509. force_new_loop: bool = False,
  510. ) -> (
  511. Callable[
  512. [Callable[_P, Coroutine[Any, Any, _R]] | Callable[_P, Awaitable[_R]]],
  513. Callable[_P, _R],
  514. ]
  515. | Callable[_P, _R]
  516. ):
  517. if awaitable is None:
  518. return lambda f: AsyncToSync(
  519. f,
  520. force_new_loop=force_new_loop,
  521. )
  522. return AsyncToSync(
  523. awaitable,
  524. force_new_loop=force_new_loop,
  525. )
  526. @overload
  527. def sync_to_async(
  528. *,
  529. thread_sensitive: bool = True,
  530. executor: Optional["ThreadPoolExecutor"] = None,
  531. context: contextvars.Context | None = None,
  532. ) -> Callable[[Callable[_P, _R]], Callable[_P, Coroutine[Any, Any, _R]]]: ...
  533. @overload
  534. def sync_to_async(
  535. func: Callable[_P, _R],
  536. *,
  537. thread_sensitive: bool = True,
  538. executor: Optional["ThreadPoolExecutor"] = None,
  539. context: contextvars.Context | None = None,
  540. ) -> Callable[_P, Coroutine[Any, Any, _R]]: ...
  541. def sync_to_async(
  542. func: Callable[_P, _R] | None = None,
  543. *,
  544. thread_sensitive: bool = True,
  545. executor: Optional["ThreadPoolExecutor"] = None,
  546. context: contextvars.Context | None = None,
  547. ) -> (
  548. Callable[[Callable[_P, _R]], Callable[_P, Coroutine[Any, Any, _R]]]
  549. | Callable[_P, Coroutine[Any, Any, _R]]
  550. ):
  551. if func is None:
  552. return lambda f: SyncToAsync(
  553. f,
  554. thread_sensitive=thread_sensitive,
  555. executor=executor,
  556. context=context,
  557. )
  558. return SyncToAsync(
  559. func,
  560. thread_sensitive=thread_sensitive,
  561. executor=executor,
  562. context=context,
  563. )