async.txt 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. ====================
  2. Asynchronous support
  3. ====================
  4. .. currentmodule:: asgiref.sync
  5. Django has support for writing asynchronous ("async") views, along with an
  6. entirely async-enabled request stack if you are running under
  7. :doc:`ASGI </howto/deployment/asgi/index>`. Async views will still work under
  8. WSGI, but with performance penalties, and without the ability to have efficient
  9. long-running requests.
  10. We're still working on async support for the ORM and other parts of Django.
  11. You can expect to see this in future releases. For now, you can use the
  12. :func:`sync_to_async` adapter to interact with the sync parts of Django.
  13. There is also a whole range of async-native Python libraries that you can
  14. integrate with.
  15. Async views
  16. ===========
  17. Any view can be declared async by making the callable part of it return a
  18. coroutine - commonly, this is done using ``async def``. For a function-based
  19. view, this means declaring the whole view using ``async def``. For a
  20. class-based view, this means declaring the HTTP method handlers, such as
  21. ``get()`` and ``post()`` as ``async def`` (not its ``__init__()``, or
  22. ``as_view()``).
  23. .. note::
  24. Django uses ``asgiref.sync.iscoroutinefunction`` to test if your view is
  25. asynchronous or not. If you implement your own method of returning a
  26. coroutine, ensure you use ``asgiref.sync.markcoroutinefunction`` so this
  27. function returns ``True``.
  28. Under a WSGI server, async views will run in their own, one-off event loop.
  29. This means you can use async features, like concurrent async HTTP requests,
  30. without any issues, but you will not get the benefits of an async stack.
  31. The main benefits are the ability to service hundreds of connections without
  32. using Python threads. This allows you to use slow streaming, long-polling, and
  33. other exciting response types.
  34. If you want to use these, you will need to deploy Django using
  35. :doc:`ASGI </howto/deployment/asgi/index>` instead.
  36. .. warning::
  37. You will only get the benefits of a fully-asynchronous request stack if you
  38. have *no synchronous middleware* loaded into your site. If there is a piece
  39. of synchronous middleware, then Django must use a thread per request to
  40. safely emulate a synchronous environment for it.
  41. Middleware can be built to support :ref:`both sync and async
  42. <async-middleware>` contexts. Some of Django's middleware is built like
  43. this, but not all. To see what middleware Django has to adapt for, you can
  44. turn on debug logging for the ``django.request`` logger and look for log
  45. messages about *"Asynchronous handler adapted for middleware ..."*.
  46. In both ASGI and WSGI mode, you can still safely use asynchronous support to
  47. run code concurrently rather than serially. This is especially handy when
  48. dealing with external APIs or data stores.
  49. If you want to call a part of Django that is still synchronous, you will need
  50. to wrap it in a :func:`sync_to_async` call. For example::
  51. from asgiref.sync import sync_to_async
  52. results = await sync_to_async(sync_function, thread_sensitive=True)(pk=123)
  53. If you accidentally try to call a part of Django that is synchronous-only
  54. from an async view, you will trigger Django's
  55. :ref:`asynchronous safety protection <async-safety>` to protect your data from
  56. corruption.
  57. Decorators
  58. ----------
  59. .. versionadded:: 5.0
  60. The following decorators can be used with both synchronous and asynchronous
  61. view functions:
  62. * :func:`~django.views.decorators.cache.cache_control`
  63. * :func:`~django.views.decorators.cache.never_cache`
  64. * :func:`~django.views.decorators.common.no_append_slash`
  65. * :func:`~django.views.decorators.csrf.csrf_exempt`
  66. * :func:`~django.views.decorators.debug.sensitive_variables`
  67. * :func:`~django.views.decorators.debug.sensitive_post_parameters`
  68. * :func:`~django.views.decorators.http.condition`
  69. * :func:`~django.views.decorators.http.etag`
  70. * :func:`~django.views.decorators.http.last_modified`
  71. * :func:`~django.views.decorators.http.require_http_methods`
  72. * :func:`~django.views.decorators.http.require_GET`
  73. * :func:`~django.views.decorators.http.require_POST`
  74. * :func:`~django.views.decorators.http.require_safe`
  75. * :func:`~django.views.decorators.vary.vary_on_cookie`
  76. * :func:`~django.views.decorators.vary.vary_on_headers`
  77. * ``xframe_options_deny()``
  78. * ``xframe_options_sameorigin()``
  79. * ``xframe_options_exempt()``
  80. For example::
  81. from django.views.decorators.cache import never_cache
  82. @never_cache
  83. def my_sync_view(request):
  84. ...
  85. @never_cache
  86. async def my_async_view(request):
  87. ...
  88. Queries & the ORM
  89. -----------------
  90. With some exceptions, Django can run ORM queries asynchronously as well::
  91. async for author in Author.objects.filter(name__startswith="A"):
  92. book = await author.books.afirst()
  93. Detailed notes can be found in :ref:`async-queries`, but in short:
  94. * All ``QuerySet`` methods that cause an SQL query to occur have an
  95. ``a``-prefixed asynchronous variant.
  96. * ``async for`` is supported on all QuerySets (including the output of
  97. ``values()`` and ``values_list()``.)
  98. Django also supports some asynchronous model methods that use the database::
  99. async def make_book(*args, **kwargs):
  100. book = Book(...)
  101. await book.asave(using="secondary")
  102. async def make_book_with_tags(tags, *args, **kwargs):
  103. book = await Book.objects.acreate(...)
  104. await book.tags.aset(tags)
  105. Transactions do not yet work in async mode. If you have a piece of code that
  106. needs transactions behavior, we recommend you write that piece as a single
  107. synchronous function and call it using :func:`sync_to_async`.
  108. .. versionchanged:: 4.2
  109. Asynchronous model and related manager interfaces were added.
  110. .. _async_performance:
  111. Performance
  112. -----------
  113. When running in a mode that does not match the view (e.g. an async view under
  114. WSGI, or a traditional sync view under ASGI), Django must emulate the other
  115. call style to allow your code to run. This context-switch causes a small
  116. performance penalty of around a millisecond.
  117. This is also true of middleware. Django will attempt to minimize the number of
  118. context-switches between sync and async. If you have an ASGI server, but all
  119. your middleware and views are synchronous, it will switch just once, before it
  120. enters the middleware stack.
  121. However, if you put synchronous middleware between an ASGI server and an
  122. asynchronous view, it will have to switch into sync mode for the middleware and
  123. then back to async mode for the view. Django will also hold the sync thread
  124. open for middleware exception propagation. This may not be noticeable at first,
  125. but adding this penalty of one thread per request can remove any async
  126. performance advantage.
  127. You should do your own performance testing to see what effect ASGI versus WSGI
  128. has on your code. In some cases, there may be a performance increase even for
  129. a purely synchronous codebase under ASGI because the request-handling code is
  130. still all running asynchronously. In general you will only want to enable ASGI
  131. mode if you have asynchronous code in your project.
  132. .. _async-handling-disconnect:
  133. Handling disconnects
  134. --------------------
  135. .. versionadded:: 5.0
  136. For long-lived requests, a client may disconnect before the view returns a
  137. response. In this case, an ``asyncio.CancelledError`` will be raised in the
  138. view. You can catch this error and handle it if you need to perform any
  139. cleanup::
  140. async def my_view(request):
  141. try:
  142. # Do some work
  143. ...
  144. except asyncio.CancelledError:
  145. # Handle disconnect
  146. raise
  147. You can also :ref:`handle client disconnects in streaming responses
  148. <request-response-streaming-disconnect>`.
  149. .. _async-safety:
  150. Async safety
  151. ============
  152. .. envvar:: DJANGO_ALLOW_ASYNC_UNSAFE
  153. Certain key parts of Django are not able to operate safely in an async
  154. environment, as they have global state that is not coroutine-aware. These parts
  155. of Django are classified as "async-unsafe", and are protected from execution in
  156. an async environment. The ORM is the main example, but there are other parts
  157. that are also protected in this way.
  158. If you try to run any of these parts from a thread where there is a *running
  159. event loop*, you will get a
  160. :exc:`~django.core.exceptions.SynchronousOnlyOperation` error. Note that you
  161. don't have to be inside an async function directly to have this error occur. If
  162. you have called a sync function directly from an async function,
  163. without using :func:`sync_to_async` or similar, then it can also occur. This is
  164. because your code is still running in a thread with an active event loop, even
  165. though it may not be declared as async code.
  166. If you encounter this error, you should fix your code to not call the offending
  167. code from an async context. Instead, write your code that talks to async-unsafe
  168. functions in its own, sync function, and call that using
  169. :func:`asgiref.sync.sync_to_async` (or any other way of running sync code in
  170. its own thread).
  171. The async context can be imposed upon you by the environment in which you are
  172. running your Django code. For example, Jupyter_ notebooks and IPython_
  173. interactive shells both transparently provide an active event loop so that it is
  174. easier to interact with asynchronous APIs.
  175. If you're using an IPython shell, you can disable this event loop by running:
  176. .. code-block:: shell
  177. %autoawait off
  178. as a command at the IPython prompt. This will allow you to run synchronous code
  179. without generating :exc:`~django.core.exceptions.SynchronousOnlyOperation`
  180. errors; however, you also won't be able to ``await`` asynchronous APIs. To turn
  181. the event loop back on, run:
  182. .. code-block:: shell
  183. %autoawait on
  184. If you're in an environment other than IPython (or you can't turn off
  185. ``autoawait`` in IPython for some reason), you are *certain* there is no chance
  186. of your code being run concurrently, and you *absolutely* need to run your sync
  187. code from an async context, then you can disable the warning by setting the
  188. :envvar:`DJANGO_ALLOW_ASYNC_UNSAFE` environment variable to any value.
  189. .. warning::
  190. If you enable this option and there is concurrent access to the
  191. async-unsafe parts of Django, you may suffer data loss or corruption. Be
  192. very careful and do not use this in production environments.
  193. If you need to do this from within Python, do that with ``os.environ``::
  194. import os
  195. os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"
  196. .. _Jupyter: https://jupyter.org/
  197. .. _IPython: https://ipython.org
  198. Async adapter functions
  199. =======================
  200. It is necessary to adapt the calling style when calling sync code from an async
  201. context, or vice-versa. For this there are two adapter functions, from the
  202. ``asgiref.sync`` module: :func:`async_to_sync` and :func:`sync_to_async`. They
  203. are used to transition between the calling styles while preserving
  204. compatibility.
  205. These adapter functions are widely used in Django. The :pypi:`asgiref` package
  206. itself is part of the Django project, and it is automatically installed as a
  207. dependency when you install Django with ``pip``.
  208. ``async_to_sync()``
  209. -------------------
  210. .. function:: async_to_sync(async_function, force_new_loop=False)
  211. Takes an async function and returns a sync function that wraps it. Can be used
  212. as either a direct wrapper or a decorator::
  213. from asgiref.sync import async_to_sync
  214. async def get_data():
  215. ...
  216. sync_get_data = async_to_sync(get_data)
  217. @async_to_sync
  218. async def get_other_data():
  219. ...
  220. The async function is run in the event loop for the current thread, if one is
  221. present. If there is no current event loop, a new event loop is spun up
  222. specifically for the single async invocation and shut down again once it
  223. completes. In either situation, the async function will execute on a different
  224. thread to the calling code.
  225. Threadlocals and contextvars values are preserved across the boundary in both
  226. directions.
  227. :func:`async_to_sync` is essentially a more powerful version of the
  228. :py:func:`asyncio.run` function in Python's standard library. As well
  229. as ensuring threadlocals work, it also enables the ``thread_sensitive`` mode of
  230. :func:`sync_to_async` when that wrapper is used below it.
  231. ``sync_to_async()``
  232. -------------------
  233. .. function:: sync_to_async(sync_function, thread_sensitive=True)
  234. Takes a sync function and returns an async function that wraps it. Can be used
  235. as either a direct wrapper or a decorator::
  236. from asgiref.sync import sync_to_async
  237. async_function = sync_to_async(sync_function, thread_sensitive=False)
  238. async_function = sync_to_async(sensitive_sync_function, thread_sensitive=True)
  239. @sync_to_async
  240. def sync_function():
  241. ...
  242. Threadlocals and contextvars values are preserved across the boundary in both
  243. directions.
  244. Sync functions tend to be written assuming they all run in the main
  245. thread, so :func:`sync_to_async` has two threading modes:
  246. * ``thread_sensitive=True`` (the default): the sync function will run in the
  247. same thread as all other ``thread_sensitive`` functions. This will be the
  248. main thread, if the main thread is synchronous and you are using the
  249. :func:`async_to_sync` wrapper.
  250. * ``thread_sensitive=False``: the sync function will run in a brand new thread
  251. which is then closed once the invocation completes.
  252. .. warning::
  253. ``asgiref`` version 3.3.0 changed the default value of the
  254. ``thread_sensitive`` parameter to ``True``. This is a safer default, and in
  255. many cases interacting with Django the correct value, but be sure to
  256. evaluate uses of ``sync_to_async()`` if updating ``asgiref`` from a prior
  257. version.
  258. Thread-sensitive mode is quite special, and does a lot of work to run all
  259. functions in the same thread. Note, though, that it *relies on usage of*
  260. :func:`async_to_sync` *above it in the stack* to correctly run things on the
  261. main thread. If you use ``asyncio.run()`` or similar, it will fall back to
  262. running thread-sensitive functions in a single, shared thread, but this will
  263. not be the main thread.
  264. The reason this is needed in Django is that many libraries, specifically
  265. database adapters, require that they are accessed in the same thread that they
  266. were created in. Also a lot of existing Django code assumes it all runs in the
  267. same thread, e.g. middleware adding things to a request for later use in views.
  268. Rather than introduce potential compatibility issues with this code, we instead
  269. opted to add this mode so that all existing Django sync code runs in the same
  270. thread and thus is fully compatible with async mode. Note that sync code will
  271. always be in a *different* thread to any async code that is calling it, so you
  272. should avoid passing raw database handles or other thread-sensitive references
  273. around.
  274. In practice this restriction means that you should not pass features of the
  275. database ``connection`` object when calling ``sync_to_async()``. Doing so will
  276. trigger the thread safety checks:
  277. .. code-block:: pycon
  278. # DJANGO_SETTINGS_MODULE=settings.py python -m asyncio
  279. >>> import asyncio
  280. >>> from asgiref.sync import sync_to_async
  281. >>> from django.db import connection
  282. >>> # In an async context so you cannot use the database directly:
  283. >>> connection.cursor()
  284. django.core.exceptions.SynchronousOnlyOperation: You cannot call this from
  285. an async context - use a thread or sync_to_async.
  286. >>> # Nor can you pass resolved connection attributes across threads:
  287. >>> await sync_to_async(connection.cursor)()
  288. django.db.utils.DatabaseError: DatabaseWrapper objects created in a thread
  289. can only be used in that same thread. The object with alias 'default' was
  290. created in thread id 4371465600 and this is thread id 6131478528.
  291. Rather, you should encapsulate all database access within a helper function
  292. that can be called with ``sync_to_async()`` without relying on the connection
  293. object in the calling code.
  294. Use with exception reporting filters
  295. ------------------------------------
  296. .. warning::
  297. Due to the machinery needed to cross the sync/async boundary,
  298. ``sync_to_async()`` and ``async_to_sync()`` are **not** compatible with
  299. :func:`~django.views.decorators.debug.sensitive_variables`, used to mask
  300. local variables from exception reports.
  301. If using these adapters with sensitive variables, ensure to audit exception
  302. reporting, and consider implementing a :ref:`custom filter
  303. <custom-error-reports>` if necessary.