METADATA 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. Metadata-Version: 2.4
  2. Name: asgiref
  3. Version: 3.11.0
  4. Summary: ASGI specs, helper code, and adapters
  5. Home-page: https://github.com/django/asgiref/
  6. Author: Django Software Foundation
  7. Author-email: foundation@djangoproject.com
  8. License: BSD-3-Clause
  9. Project-URL: Documentation, https://asgi.readthedocs.io/
  10. Project-URL: Further Documentation, https://docs.djangoproject.com/en/stable/topics/async/#async-adapter-functions
  11. Project-URL: Changelog, https://github.com/django/asgiref/blob/master/CHANGELOG.txt
  12. Classifier: Development Status :: 5 - Production/Stable
  13. Classifier: Environment :: Web Environment
  14. Classifier: Intended Audience :: Developers
  15. Classifier: License :: OSI Approved :: BSD License
  16. Classifier: Operating System :: OS Independent
  17. Classifier: Programming Language :: Python
  18. Classifier: Programming Language :: Python :: 3
  19. Classifier: Programming Language :: Python :: 3 :: Only
  20. Classifier: Programming Language :: Python :: 3.9
  21. Classifier: Programming Language :: Python :: 3.10
  22. Classifier: Programming Language :: Python :: 3.11
  23. Classifier: Programming Language :: Python :: 3.12
  24. Classifier: Programming Language :: Python :: 3.13
  25. Classifier: Topic :: Internet :: WWW/HTTP
  26. Requires-Python: >=3.9
  27. License-File: LICENSE
  28. Requires-Dist: typing_extensions>=4; python_version < "3.11"
  29. Provides-Extra: tests
  30. Requires-Dist: pytest; extra == "tests"
  31. Requires-Dist: pytest-asyncio; extra == "tests"
  32. Requires-Dist: mypy>=1.14.0; extra == "tests"
  33. Dynamic: license-file
  34. asgiref
  35. =======
  36. .. image:: https://github.com/django/asgiref/actions/workflows/tests.yml/badge.svg
  37. :target: https://github.com/django/asgiref/actions/workflows/tests.yml
  38. .. image:: https://img.shields.io/pypi/v/asgiref.svg
  39. :target: https://pypi.python.org/pypi/asgiref
  40. ASGI is a standard for Python asynchronous web apps and servers to communicate
  41. with each other, and positioned as an asynchronous successor to WSGI. You can
  42. read more at https://asgi.readthedocs.io/en/latest/
  43. This package includes ASGI base libraries, such as:
  44. * Sync-to-async and async-to-sync function wrappers, ``asgiref.sync``
  45. * Server base classes, ``asgiref.server``
  46. * A WSGI-to-ASGI adapter, in ``asgiref.wsgi``
  47. Function wrappers
  48. -----------------
  49. These allow you to wrap or decorate async or sync functions to call them from
  50. the other style (so you can call async functions from a synchronous thread,
  51. or vice-versa).
  52. In particular:
  53. * AsyncToSync lets a synchronous subthread stop and wait while the async
  54. function is called on the main thread's event loop, and then control is
  55. returned to the thread when the async function is finished.
  56. * SyncToAsync lets async code call a synchronous function, which is run in
  57. a threadpool and control returned to the async coroutine when the synchronous
  58. function completes.
  59. The idea is to make it easier to call synchronous APIs from async code and
  60. asynchronous APIs from synchronous code so it's easier to transition code from
  61. one style to the other. In the case of Channels, we wrap the (synchronous)
  62. Django view system with SyncToAsync to allow it to run inside the (asynchronous)
  63. ASGI server.
  64. Note that exactly what threads things run in is very specific, and aimed to
  65. keep maximum compatibility with old synchronous code. See
  66. "Synchronous code & Threads" below for a full explanation. By default,
  67. ``sync_to_async`` will run all synchronous code in the program in the same
  68. thread for safety reasons; you can disable this for more performance with
  69. ``@sync_to_async(thread_sensitive=False)``, but make sure that your code does
  70. not rely on anything bound to threads (like database connections) when you do.
  71. Threadlocal replacement
  72. -----------------------
  73. This is a drop-in replacement for ``threading.local`` that works with both
  74. threads and asyncio Tasks. Even better, it will proxy values through from a
  75. task-local context to a thread-local context when you use ``sync_to_async``
  76. to run things in a threadpool, and vice-versa for ``async_to_sync``.
  77. If you instead want true thread- and task-safety, you can set
  78. ``thread_critical`` on the Local object to ensure this instead.
  79. Server base classes
  80. -------------------
  81. Includes a ``StatelessServer`` class which provides all the hard work of
  82. writing a stateless server (as in, does not handle direct incoming sockets
  83. but instead consumes external streams or sockets to work out what is happening).
  84. An example of such a server would be a chatbot server that connects out to
  85. a central chat server and provides a "connection scope" per user chatting to
  86. it. There's only one actual connection, but the server has to separate things
  87. into several scopes for easier writing of the code.
  88. You can see an example of this being used in `frequensgi <https://github.com/andrewgodwin/frequensgi>`_.
  89. WSGI-to-ASGI adapter
  90. --------------------
  91. Allows you to wrap a WSGI application so it appears as a valid ASGI application.
  92. Simply wrap it around your WSGI application like so::
  93. asgi_application = WsgiToAsgi(wsgi_application)
  94. The WSGI application will be run in a synchronous threadpool, and the wrapped
  95. ASGI application will be one that accepts ``http`` class messages.
  96. Please note that not all extended features of WSGI may be supported (such as
  97. file handles for incoming POST bodies).
  98. Dependencies
  99. ------------
  100. ``asgiref`` requires Python 3.9 or higher.
  101. Contributing
  102. ------------
  103. Please refer to the
  104. `main Channels contributing docs <https://github.com/django/channels/blob/master/CONTRIBUTING.rst>`_.
  105. Testing
  106. '''''''
  107. To run tests, make sure you have installed the ``tests`` extra with the package::
  108. cd asgiref/
  109. pip install -e .[tests]
  110. pytest
  111. Building the documentation
  112. ''''''''''''''''''''''''''
  113. The documentation uses `Sphinx <http://www.sphinx-doc.org>`_::
  114. cd asgiref/docs/
  115. pip install sphinx
  116. To build the docs, you can use the default tools::
  117. sphinx-build -b html . _build/html # or `make html`, if you've got make set up
  118. cd _build/html
  119. python -m http.server
  120. ...or you can use ``sphinx-autobuild`` to run a server and rebuild/reload
  121. your documentation changes automatically::
  122. pip install sphinx-autobuild
  123. sphinx-autobuild . _build/html
  124. Releasing
  125. '''''''''
  126. To release, first add details to CHANGELOG.txt and update the version number in ``asgiref/__init__.py``.
  127. Then, build and push the packages::
  128. python -m build
  129. twine upload dist/*
  130. rm -r asgiref.egg-info dist
  131. Implementation Details
  132. ----------------------
  133. Synchronous code & threads
  134. ''''''''''''''''''''''''''
  135. The ``asgiref.sync`` module provides two wrappers that let you go between
  136. asynchronous and synchronous code at will, while taking care of the rough edges
  137. for you.
  138. Unfortunately, the rough edges are numerous, and the code has to work especially
  139. hard to keep things in the same thread as much as possible. Notably, the
  140. restrictions we are working with are:
  141. * All synchronous code called through ``SyncToAsync`` and marked with
  142. ``thread_sensitive`` should run in the same thread as each other (and if the
  143. outer layer of the program is synchronous, the main thread)
  144. * If a thread already has a running async loop, ``AsyncToSync`` can't run things
  145. on that loop if it's blocked on synchronous code that is above you in the
  146. call stack.
  147. The first compromise you get to might be that ``thread_sensitive`` code should
  148. just run in the same thread and not spawn in a sub-thread, fulfilling the first
  149. restriction, but that immediately runs you into the second restriction.
  150. The only real solution is to essentially have a variant of ThreadPoolExecutor
  151. that executes any ``thread_sensitive`` code on the outermost synchronous
  152. thread - either the main thread, or a single spawned subthread.
  153. This means you now have two basic states:
  154. * If the outermost layer of your program is synchronous, then all async code
  155. run through ``AsyncToSync`` will run in a per-call event loop in arbitrary
  156. sub-threads, while all ``thread_sensitive`` code will run in the main thread.
  157. * If the outermost layer of your program is asynchronous, then all async code
  158. runs on the main thread's event loop, and all ``thread_sensitive`` synchronous
  159. code will run in a single shared sub-thread.
  160. Crucially, this means that in both cases there is a thread which is a shared
  161. resource that all ``thread_sensitive`` code must run on, and there is a chance
  162. that this thread is currently blocked on its own ``AsyncToSync`` call. Thus,
  163. ``AsyncToSync`` needs to act as an executor for thread code while it's blocking.
  164. The ``CurrentThreadExecutor`` class provides this functionality; rather than
  165. simply waiting on a Future, you can call its ``run_until_future`` method and
  166. it will run submitted code until that Future is done. This means that code
  167. inside the call can then run code on your thread.
  168. Maintenance and Security
  169. ------------------------
  170. To report security issues, please contact security@djangoproject.com. For GPG
  171. signatures and more security process information, see
  172. https://docs.djangoproject.com/en/dev/internals/security/.
  173. To report bugs or request new features, please open a new GitHub issue.
  174. This repository is part of the Channels project. For the shepherd and maintenance team, please see the
  175. `main Channels readme <https://github.com/django/channels/blob/master/README.rst>`_.