METADATA 8.9 KB

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