### Install asgiref with Tests Source: https://github.com/django/asgiref/blob/main/README.rst To run tests for asgiref, install the package with the 'tests' extra. This command should be executed from the asgiref/ directory. ```bash cd asgiref/ pip install -e .[tests] pytest ``` -------------------------------- ### HTTP Request Event Example Source: https://github.com/django/asgiref/blob/main/docs/introduction.md An example of an event dictionary received from the 'receive' callable, representing an HTTP request. ```json { "type": "http.request", "body": b"Hello World", "more_body": False, } ``` -------------------------------- ### ASGI HTTP Extension Example Source: https://github.com/django/asgiref/blob/main/docs/specs/main.md This example shows how an HTTP protocol server can signal support for a custom 'fullflush' extension by including an empty dictionary for it in the scope's extensions. Applications can then use this to send custom events. ```default scope = { "type": "http", "method": "GET", ... "extensions": { "fullflush": {}, }, } ``` -------------------------------- ### ASGI Application Lifespan Implementation Source: https://github.com/django/asgiref/blob/main/specs/lifespan.rst Example of an ASGI application handling lifespan startup and shutdown events. The application must send 'lifespan.startup.complete' or 'lifespan.shutdown.complete' upon successful completion. ```python async def app(scope, receive, send): if scope['type'] == 'lifespan': while True: message = await receive() if message['type'] == 'lifespan.startup': ... # Do some startup here! await send({'type': 'lifespan.startup.complete'}) elif message['type'] == 'lifespan.shutdown': ... # Do some shutdown here! await send({'type': 'lifespan.shutdown.complete'}) return else: pass # Handle other types ``` -------------------------------- ### WebSocket Send Event Example Source: https://github.com/django/asgiref/blob/main/docs/introduction.md An example of an event dictionary passed to the 'send' callable to transmit an outgoing WebSocket message. ```json { "type": "websocket.send", "text": "Hello world!", } ``` -------------------------------- ### Lifespan Scope State Example Source: https://github.com/django/asgiref/blob/main/docs/specs/lifespan.md Illustrates the structure of the lifespan scope, specifically highlighting the 'state' dictionary. This namespace is used by applications to persist data across the lifespan and request/response cycles. ```default "scope": { ... "state": {}, } ``` -------------------------------- ### Build Sphinx Documentation Source: https://github.com/django/asgiref/blob/main/README.rst Build the Sphinx documentation by navigating to the docs directory and running the sphinx-build command. The output will be in the _build/html directory. ```bash cd asgiref/docs/ pip install sphinx sphinx-build -b html . _build/html ``` -------------------------------- ### Serve Sphinx Documentation Locally Source: https://github.com/django/asgiref/blob/main/README.rst After building the Sphinx documentation, you can serve it locally using Python's http.server module. Navigate to the build directory first. ```bash cd _build/html python -m http.server ``` -------------------------------- ### Basic ASGI Application Structure Source: https://github.com/django/asgiref/blob/main/docs/introduction.md A minimal ASGI application written as an asynchronous function. It demonstrates receiving an event and sending a response. ```python async def application(scope, receive, send): event = await receive() ... await send({"type": "websocket.send", ...}) ``` -------------------------------- ### Build and Release asgiref Packages Source: https://github.com/django/asgiref/blob/main/README.rst To release a new version of asgiref, update the changelog and version number, then build and upload the distribution packages using the build and twine commands. ```bash python -m build twine upload dist/* rm -r asgiref.egg-info dist ``` -------------------------------- ### Wrap WSGI Application with WsgiToAsgi Source: https://github.com/django/asgiref/blob/main/README.rst Use WsgiToAsgi to wrap a WSGI application, making it appear as a valid ASGI application. The WSGI application will run in a synchronous threadpool. ```python asgi_application = WsgiToAsgi(wsgi_application) ``` -------------------------------- ### Use asgiref.sync iscoroutinefunction and markcoroutinefunction Source: https://github.com/django/asgiref/blob/main/CHANGELOG.txt These functions are compatibility shims for asyncio.iscoroutinefunction and inspect.markcoroutinefunction, intended for use until Python 3.12 is the minimum supported version. They are considered beta and subject to change. ```python from asgiref.sync import iscoroutinefunction, markcoroutinefunction # Example usage: async def my_coro(): pass if iscoroutinefunction(my_coro): print("It's a coroutine function") @markcoroutinefunction def regular_function(): pass ``` -------------------------------- ### Early Hint Extension Scope Source: https://github.com/django/asgiref/blob/main/docs/extensions.md This illustrates the scope object when the http.response.early_hint extension is supported by the ASGI server. ```json "scope": { ... "extensions": { "http.response.early_hint": {}, }, } ``` -------------------------------- ### HTTP Response Path Send Message Source: https://github.com/django/asgiref/blob/main/docs/extensions.md An ASGI framework initiates a path-send by sending this message. It specifies the absolute file path to be sent by the server. ```json { "type": "http.response.pathsend", "path": "/path/to/file" } ``` -------------------------------- ### Zero Copy Send Message Structure Source: https://github.com/django/asgiref/blob/main/docs/extensions.md Details the message structure for initiating a zero-copy send operation in ASGI. ```default { "type": "http.response.zerocopysend", "file": "file descriptor object", "offset": "int", "count": "int", "more_body": "bool" } ``` -------------------------------- ### HTTP/2 Server Push Extension Scope Source: https://github.com/django/asgiref/blob/main/docs/extensions.md This indicates the presence of the http.response.push extension in the ASGI server's scope. ```default "scope": { ... "extensions": { "http.response.push": {}, }, } ``` -------------------------------- ### Legacy ASGI Application Callable Source: https://github.com/django/asgiref/blob/main/docs/specs/main.md Defines the structure of a legacy (v2.0) ASGI application, which is a synchronous callable that returns an awaitable callable for connection handling. ```default application(scope) ``` -------------------------------- ### Configure WsgiToAsgi with duplicate_header_limit Source: https://github.com/django/asgiref/blob/main/CHANGELOG.txt Override the duplicate_header_limit parameter when configuring your WsgiToAsgi application to control the maximum number of times a header can be repeated. Set to None to disable the check. ```python application = WsgiToAsgi(wsgi_app, duplicate_header_limit=200) ``` -------------------------------- ### HTTP Response Early Hint Message Source: https://github.com/django/asgiref/blob/main/docs/extensions.md An ASGI framework sends an early hint using this message, providing an iterable of link header field values. ```json { "type": "http.response.early_hint", "links": [ "; rel=preload; as=style", "; rel=preload; as=script" ] } ``` -------------------------------- ### Zero Copy Send Extension Scope Source: https://github.com/django/asgiref/blob/main/docs/extensions.md This shows the scope configuration when the http.response.zerocopysend extension is available on an ASGI server. ```default "scope": { ... "extensions": { "http.response.zerocopysend": {}, }, } ``` -------------------------------- ### Legacy ASGI Application Signature Source: https://github.com/django/asgiref/blob/main/specs/asgi.rst Defines the signature for a legacy ASGI application callable (pre-version 3.0). It involves an initial synchronous callable returning an awaitable callable. ```python application(scope) ``` ```python coroutine application_instance(receive, send) ``` -------------------------------- ### HTTP/2 Server Push Message Structure Source: https://github.com/django/asgiref/blob/main/docs/extensions.md Defines the structure of the message an ASGI framework sends to initiate an HTTP/2 server push. ```default { "type": "http.response.push", "path": "Unicode string", "headers": "Iterable[[byte string, byte string]]" } ``` -------------------------------- ### Websocket Denial Response Extension Scope Source: https://github.com/django/asgiref/blob/main/docs/extensions.md This shows the structure of the scope when the websocket.http.response extension is implemented by an ASGI server. ```default "scope": { ... "extensions": { "websocket.http.response": {}, }, } ``` -------------------------------- ### Path Send Extension Scope Source: https://github.com/django/asgiref/blob/main/docs/extensions.md This shows the structure of the scope object when the http.response.pathsend extension is available. ```json "scope": { ... "extensions": { "http.response.pathsend": {}, }, } ``` -------------------------------- ### Legacy ASGI Application Instance Coroutine Source: https://github.com/django/asgiref/blob/main/docs/specs/main.md Represents the awaitable callable returned by a legacy ASGI application, responsible for handling receive and send operations. ```default coroutine application_instance(receive, send) ``` -------------------------------- ### HTTP Response Trailers Message Source: https://github.com/django/asgiref/blob/main/docs/extensions.md An ASGI framework sends trailing headers to the client using this message. It includes the headers and a flag indicating if more trailers are expected. ```json { "type": "http.response.trailers", "headers": [ [b"custom-header", b"value"] ], "more_trailers": false } ``` -------------------------------- ### HTTP Response Debug Message Source: https://github.com/django/asgiref/blob/main/docs/extensions.md An ASGI framework sends debug information to the server using this message. The 'info' dictionary can contain arbitrary key-value pairs. ```json { "type": "http.response.debug", "info": { "message": "Processing request", "details": {"user_id": 123} } } ``` -------------------------------- ### ASGI Application Callable Signature Source: https://github.com/django/asgiref/blob/main/docs/specs/main.md The standard signature for an ASGI application callable. It takes the connection scope, a receive callable, and a send callable. ```python coroutine application(scope, receive, send) ``` -------------------------------- ### HTTP Trailers Extension Scope Source: https://github.com/django/asgiref/blob/main/docs/extensions.md This shows the scope object when the http.response.trailers extension is available, indicating server support for trailing headers. ```json "scope": { ... "extensions": { "http.response.trailers": {}, }, } ``` -------------------------------- ### Add AsyncSingleThreadContext for AsyncToSync Source: https://github.com/django/asgiref/blob/main/CHANGELOG.txt Use the AsyncSingleThreadContext context manager to ensure multiple AsyncToSync invocations share the same thread. This is useful for maintaining thread consistency across asynchronous operations. ```python from asgiref.sync import AsyncSingleThreadContext async def my_async_function(): async with AsyncSingleThreadContext(): # Code that uses AsyncToSync pass ``` -------------------------------- ### Debug Extension Scope Source: https://github.com/django/asgiref/blob/main/docs/extensions.md This indicates the scope object when the http.response.debug extension is provided, allowing frameworks to send debug information. ```json "scope": { ... "extensions": { "http.response.debug": {}, }, } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.