You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sync.py 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. import asyncio
  2. import asyncio.coroutines
  3. import functools
  4. import os
  5. import sys
  6. import threading
  7. from concurrent.futures import Future, ThreadPoolExecutor
  8. from .current_thread_executor import CurrentThreadExecutor
  9. from .local import Local
  10. try:
  11. import contextvars # Python 3.7+ only.
  12. except ImportError:
  13. contextvars = None
  14. class AsyncToSync:
  15. """
  16. Utility class which turns an awaitable that only works on the thread with
  17. the event loop into a synchronous callable that works in a subthread.
  18. If the call stack contains an async loop, the code runs there.
  19. Otherwise, the code runs in a new loop in a new thread.
  20. Either way, this thread then pauses and waits to run any thread_sensitive
  21. code called from further down the call stack using SyncToAsync, before
  22. finally exiting once the async task returns.
  23. """
  24. # Maps launched Tasks to the threads that launched them (for locals impl)
  25. launch_map = {}
  26. # Keeps track of which CurrentThreadExecutor to use. This uses an asgiref
  27. # Local, not a threadlocal, so that tasks can work out what their parent used.
  28. executors = Local()
  29. def __init__(self, awaitable, force_new_loop=False):
  30. self.awaitable = awaitable
  31. if force_new_loop:
  32. # They have asked that we always run in a new sub-loop.
  33. self.main_event_loop = None
  34. else:
  35. try:
  36. self.main_event_loop = asyncio.get_event_loop()
  37. except RuntimeError:
  38. # There's no event loop in this thread. Look for the threadlocal if
  39. # we're inside SyncToAsync
  40. self.main_event_loop = getattr(
  41. SyncToAsync.threadlocal, "main_event_loop", None
  42. )
  43. def __call__(self, *args, **kwargs):
  44. # You can't call AsyncToSync from a thread with a running event loop
  45. try:
  46. event_loop = asyncio.get_event_loop()
  47. except RuntimeError:
  48. pass
  49. else:
  50. if event_loop.is_running():
  51. raise RuntimeError(
  52. "You cannot use AsyncToSync in the same thread as an async event loop - "
  53. "just await the async function directly."
  54. )
  55. # Make a future for the return information
  56. call_result = Future()
  57. # Get the source thread
  58. source_thread = threading.current_thread()
  59. # Make a CurrentThreadExecutor we'll use to idle in this thread - we
  60. # need one for every sync frame, even if there's one above us in the
  61. # same thread.
  62. if hasattr(self.executors, "current"):
  63. old_current_executor = self.executors.current
  64. else:
  65. old_current_executor = None
  66. current_executor = CurrentThreadExecutor()
  67. self.executors.current = current_executor
  68. # Use call_soon_threadsafe to schedule a synchronous callback on the
  69. # main event loop's thread if it's there, otherwise make a new loop
  70. # in this thread.
  71. try:
  72. if not (self.main_event_loop and self.main_event_loop.is_running()):
  73. # Make our own event loop - in a new thread - and run inside that.
  74. loop = asyncio.new_event_loop()
  75. loop_executor = ThreadPoolExecutor(max_workers=1)
  76. loop_future = loop_executor.submit(
  77. self._run_event_loop,
  78. loop,
  79. self.main_wrap(
  80. args, kwargs, call_result, source_thread, sys.exc_info()
  81. ),
  82. )
  83. if current_executor:
  84. # Run the CurrentThreadExecutor until the future is done
  85. current_executor.run_until_future(loop_future)
  86. # Wait for future and/or allow for exception propagation
  87. loop_future.result()
  88. else:
  89. # Call it inside the existing loop
  90. self.main_event_loop.call_soon_threadsafe(
  91. self.main_event_loop.create_task,
  92. self.main_wrap(
  93. args, kwargs, call_result, source_thread, sys.exc_info()
  94. ),
  95. )
  96. if current_executor:
  97. # Run the CurrentThreadExecutor until the future is done
  98. current_executor.run_until_future(call_result)
  99. finally:
  100. # Clean up any executor we were running
  101. if hasattr(self.executors, "current"):
  102. del self.executors.current
  103. if old_current_executor:
  104. self.executors.current = old_current_executor
  105. # Wait for results from the future.
  106. return call_result.result()
  107. def _run_event_loop(self, loop, coro):
  108. """
  109. Runs the given event loop (designed to be called in a thread).
  110. """
  111. asyncio.set_event_loop(loop)
  112. try:
  113. loop.run_until_complete(coro)
  114. finally:
  115. try:
  116. if hasattr(loop, "shutdown_asyncgens"):
  117. loop.run_until_complete(loop.shutdown_asyncgens())
  118. finally:
  119. loop.close()
  120. asyncio.set_event_loop(self.main_event_loop)
  121. def __get__(self, parent, objtype):
  122. """
  123. Include self for methods
  124. """
  125. func = functools.partial(self.__call__, parent)
  126. return functools.update_wrapper(func, self.awaitable)
  127. async def main_wrap(self, args, kwargs, call_result, source_thread, exc_info):
  128. """
  129. Wraps the awaitable with something that puts the result into the
  130. result/exception future.
  131. """
  132. current_task = SyncToAsync.get_current_task()
  133. self.launch_map[current_task] = source_thread
  134. try:
  135. # If we have an exception, run the function inside the except block
  136. # after raising it so exc_info is correctly populated.
  137. if exc_info[1]:
  138. try:
  139. raise exc_info[1]
  140. except:
  141. result = await self.awaitable(*args, **kwargs)
  142. else:
  143. result = await self.awaitable(*args, **kwargs)
  144. except Exception as e:
  145. call_result.set_exception(e)
  146. else:
  147. call_result.set_result(result)
  148. finally:
  149. del self.launch_map[current_task]
  150. class SyncToAsync:
  151. """
  152. Utility class which turns a synchronous callable into an awaitable that
  153. runs in a threadpool. It also sets a threadlocal inside the thread so
  154. calls to AsyncToSync can escape it.
  155. If thread_sensitive is passed, the code will run in the same thread as any
  156. outer code. This is needed for underlying Python code that is not
  157. threadsafe (for example, code which handles SQLite database connections).
  158. If the outermost program is async (i.e. SyncToAsync is outermost), then
  159. this will be a dedicated single sub-thread that all sync code runs in,
  160. one after the other. If the outermost program is sync (i.e. AsyncToSync is
  161. outermost), this will just be the main thread. This is achieved by idling
  162. with a CurrentThreadExecutor while AsyncToSync is blocking its sync parent,
  163. rather than just blocking.
  164. """
  165. # If they've set ASGI_THREADS, update the default asyncio executor for now
  166. if "ASGI_THREADS" in os.environ:
  167. loop = asyncio.get_event_loop()
  168. loop.set_default_executor(
  169. ThreadPoolExecutor(max_workers=int(os.environ["ASGI_THREADS"]))
  170. )
  171. # Maps launched threads to the coroutines that spawned them
  172. launch_map = {}
  173. # Storage for main event loop references
  174. threadlocal = threading.local()
  175. # Single-thread executor for thread-sensitive code
  176. single_thread_executor = ThreadPoolExecutor(max_workers=1)
  177. def __init__(self, func, thread_sensitive=False):
  178. self.func = func
  179. self._thread_sensitive = thread_sensitive
  180. self._is_coroutine = asyncio.coroutines._is_coroutine
  181. try:
  182. self.__self__ = func.__self__
  183. except AttributeError:
  184. pass
  185. async def __call__(self, *args, **kwargs):
  186. loop = asyncio.get_event_loop()
  187. # Work out what thread to run the code in
  188. if self._thread_sensitive:
  189. if hasattr(AsyncToSync.executors, "current"):
  190. # If we have a parent sync thread above somewhere, use that
  191. executor = AsyncToSync.executors.current
  192. else:
  193. # Otherwise, we run it in a fixed single thread
  194. executor = self.single_thread_executor
  195. else:
  196. executor = None # Use default
  197. if contextvars is not None:
  198. context = contextvars.copy_context()
  199. child = functools.partial(self.func, *args, **kwargs)
  200. func = context.run
  201. args = (child,)
  202. kwargs = {}
  203. else:
  204. func = self.func
  205. # Run the code in the right thread
  206. future = loop.run_in_executor(
  207. executor,
  208. functools.partial(
  209. self.thread_handler,
  210. loop,
  211. self.get_current_task(),
  212. sys.exc_info(),
  213. func,
  214. *args,
  215. **kwargs
  216. ),
  217. )
  218. return await asyncio.wait_for(future, timeout=None)
  219. def __get__(self, parent, objtype):
  220. """
  221. Include self for methods
  222. """
  223. return functools.partial(self.__call__, parent)
  224. def thread_handler(self, loop, source_task, exc_info, func, *args, **kwargs):
  225. """
  226. Wraps the sync application with exception handling.
  227. """
  228. # Set the threadlocal for AsyncToSync
  229. self.threadlocal.main_event_loop = loop
  230. # Set the task mapping (used for the locals module)
  231. current_thread = threading.current_thread()
  232. if AsyncToSync.launch_map.get(source_task) == current_thread:
  233. # Our parent task was launched from this same thread, so don't make
  234. # a launch map entry - let it shortcut over us! (and stop infinite loops)
  235. parent_set = False
  236. else:
  237. self.launch_map[current_thread] = source_task
  238. parent_set = True
  239. # Run the function
  240. try:
  241. # If we have an exception, run the function inside the except block
  242. # after raising it so exc_info is correctly populated.
  243. if exc_info[1]:
  244. try:
  245. raise exc_info[1]
  246. except:
  247. return func(*args, **kwargs)
  248. else:
  249. return func(*args, **kwargs)
  250. finally:
  251. # Only delete the launch_map parent if we set it, otherwise it is
  252. # from someone else.
  253. if parent_set:
  254. del self.launch_map[current_thread]
  255. @staticmethod
  256. def get_current_task():
  257. """
  258. Cross-version implementation of asyncio.current_task()
  259. Returns None if there is no task.
  260. """
  261. try:
  262. if hasattr(asyncio, "current_task"):
  263. # Python 3.7 and up
  264. return asyncio.current_task()
  265. else:
  266. # Python 3.6
  267. return asyncio.Task.current_task()
  268. except RuntimeError:
  269. return None
  270. # Lowercase is more sensible for most things
  271. sync_to_async = SyncToAsync
  272. async_to_sync = AsyncToSync