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 base libraries, including:

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

  • Server base classes, asgiref.server

  • 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.

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.

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

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

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-2.1.0.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

asgiref-2.1.0-py2.py3-none-any.whl (10.7 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: asgiref-2.1.0.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for asgiref-2.1.0.tar.gz
Algorithm Hash digest
SHA256 2bfd70fcc51df4036768b91d7b13524090dc8f366d79fa44ba2b0aeb47306344
MD5 764587a032612e33a9333d3be812b5dc
BLAKE2b-256 9a1a34d117e8a64e8ea91d9fd96032bc41242cc58eb8d94a8d924415d3d5282e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for asgiref-2.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 95f55cea1409db99b41fe6458b3d754712805a887b6a1ec2ebc661cc5b878555
MD5 1d19bc3ee42a30ab9a8cff550b763e1d
BLAKE2b-256 75688207f2d83ab6d6401be4f562b6b6b83b8d432438efb0eca369a8e86d4549

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