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.

METADATA 8.0KB

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