METADATA 8.9 KB

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