talira

ASGI base libraries for Python: sync-to-async wrappers, a WSGI-to-ASGI adapter and server base classes

open sourceBSD-3-Clausefree

asgiref is the Django project's package of ASGI base libraries. ASGI is the standard for Python asynchronous web apps and servers to talk to each other, the async successor to WSGI, and asgiref's repository also holds its specification. The package has the AsyncToSync and SyncToAsync wrappers that let synchronous code call async functions and the other way round (Django Channels uses SyncToAsync to run Django's synchronous views inside an ASGI server), a thread-sensitive context, a drop-in replacement for threading.local that works across both styles, a WSGI-to-ASGI adapter and base classes for ASGI servers.

Problems it solves

  • call a synchronous Django ORM function from async Python code
  • run my old WSGI app inside an ASGI server
  • call an async function from synchronous Python code without blocking the event loop

Features

  • async_to_sync and sync_to_async function wrappers
  • thread-sensitive contexts for grouping synchronous calls
  • Local: a threading.local replacement that works with async code
  • WsgiToAsgi adapter to run a WSGI app as an ASGI app
  • base classes for ASGI servers

Limitations

  • The WSGI-to-ASGI adapter may not support every extended WSGI feature, such as file handles for incoming POST bodies1