Skip to main content

ASGI specs, helper code, and adapters

Project description

https://api.travis-ci.org/django/asgiref.svg https://img.shields.io/pypi/v/asgiref.svg

ASGI is a standard for Python asynchronous web apps and servers to communicate with each other, and positioned as an asynchronous successor to WSGI. You can read more at https://asgi.readthedocs.io/en/latest/

This package includes ASGI base libraries, such as:

  • Sync-to-async and async-to-sync function wrappers, asgiref.sync

  • Server base classes, asgiref.server

  • A WSGI-to-ASGI adapter, in asgiref.wsgi

Function wrappers

These allow you to wrap or decorate async or sync functions to call them from the other style (so you can call async functions from a synchronous thread, or vice-versa).

In particular:

  • AsyncToSync lets a synchronous subthread stop and wait while the async function is called on the main thread’s event loop, and then control is returned to the thread when the async function is finished.

  • SyncToAsync lets async code call a synchronous function, which is run in a threadpool and control returned to the async coroutine when the synchronous function completes.

The idea is to make it easier to call synchronous APIs from async code and asynchronous APIs from synchronous code so it’s easier to transition code from one style to the other. In the case of Channels, we wrap the (synchronous) Django view system with SyncToAsync to allow it to run inside the (asynchronous) ASGI server.

Threadlocal replacement

This is a drop-in replacement for threading.local that works with both threads and asyncio Tasks. Even better, it will proxy values through from a task-local context to a thread-local context when you use sync_to_async to run things in a threadpool, and vice-versa for async_to_sync.

If you instead want true thread- and task-safety, you can set thread_critical on the Local object to ensure this instead.

Server base classes

Includes a StatelessServer class which provides all the hard work of writing a stateless server (as in, does not handle direct incoming sockets but instead consumes external streams or sockets to work out what is happening).

An example of such a server would be a chatbot server that connects out to a central chat server and provides a “connection scope” per user chatting to it. There’s only one actual connection, but the server has to separate things into several scopes for easier writing of the code.

You can see an example of this being used in frequensgi.

WSGI-to-ASGI adapter

Allows you to wrap a WSGI application so it appears as a valid ASGI application.

Simply wrap it around your WSGI application like so:

asgi_application = WsgiToAsgi(wsgi_application)

The WSGI application will be run in a synchronous threadpool, and the wrapped ASGI application will be one that accepts http class messages.

Please note that not all extended features of WSGI may be supported (such as file handles for incoming POST bodies).

Dependencies

asgiref requires Python 3.5 or higher.

Contributing

Please refer to the main Channels contributing docs.

Testing

To run tests, make sure you have installed the tests extra with the package:

cd asgiref/
pip install -e .[tests]
pytest

Building the documentation

The documentation uses Sphinx:

cd asgiref/docs/
pip install sphinx

To build the docs, you can use the default tools:

sphinx-build -b html . _build/html  # or `make html`, if you've got make set up
cd _build/html
python -m http.server

…or you can use sphinx-autobuild to run a server and rebuild/reload your documentation changes automatically:

pip install sphinx-autobuild
sphinx-autobuild . _build/html

Maintenance and Security

To report security issues, please contact security@djangoproject.com. For GPG signatures and more security process information, see https://docs.djangoproject.com/en/dev/internals/security/.

To report bugs or request new features, please open a new GitHub issue.

This repository is part of the Channels project. For the shepherd and maintenance team, please see the main Channels readme.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

asgiref-3.1.4.tar.gz (13.1 kB view details)

Uploaded Source

Built Distribution

asgiref-3.1.4-py2.py3-none-any.whl (15.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file asgiref-3.1.4.tar.gz.

File metadata

  • Download URL: asgiref-3.1.4.tar.gz
  • Upload date:
  • Size: 13.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.4.3 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.6

File hashes

Hashes for asgiref-3.1.4.tar.gz
Algorithm Hash digest
SHA256 865b7ccce5a6e815607b08d9059fe9c058cd75c77f896f5e0b74ff6c1ba81818
MD5 ce9b15c6f74a70990820f1e462334153
BLAKE2b-256 e44dc9079a9b0efd6755adc12413301ab6ad413d0629aa366028712ea22b0f5d

See more details on using hashes here.

File details

Details for the file asgiref-3.1.4-py2.py3-none-any.whl.

File metadata

  • Download URL: asgiref-3.1.4-py2.py3-none-any.whl
  • Upload date:
  • Size: 15.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.4.3 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.6

File hashes

Hashes for asgiref-3.1.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b718a9d35e204a96e2456c2271b0ef12e36124c363b3a8fd1d626744f23192aa
MD5 5adc873661629f62b969b53da1bef204
BLAKE2b-256 ce2edd4b5afc37d595fc44def4f365cc8ee080a4962a0eb1e05e79da65a8e074

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page