{"id":1306,"library":"a2wsgi","title":"a2wsgi","description":"a2wsgi is a Python library that enables seamless conversion between WSGI (Web Server Gateway Interface) and ASGI (Asynchronous Server Gateway Interface) applications. It acts as a middleware, allowing you to run WSGI apps on ASGI servers or ASGI apps on WSGI servers. The current version is 1.10.10, and it maintains an active release schedule with frequent minor updates, primarily focusing on bug fixes and performance improvements.","status":"active","version":"1.10.10","language":"en","source_language":"en","source_url":"https://github.com/a2wsgi/a2wsgi","tags":["ASGI","WSGI","middleware","web","server"],"install":[{"cmd":"pip install a2wsgi","lang":"bash","label":"Install a2wsgi"}],"dependencies":[],"imports":[{"symbol":"ASGIMiddleware","correct":"from a2wsgi import ASGIMiddleware"},{"symbol":"WSGIMiddleware","correct":"from a2wsgi import WSGIMiddleware"}],"quickstart":{"code":"from a2wsgi import ASGIMiddleware\n\n# A simple synchronous WSGI application\ndef wsgi_app(environ, start_response):\n    status = '200 OK'\n    headers = [('Content-type', 'text/plain; charset=utf-8')]\n    start_response(status, headers)\n    return [b\"Hello from WSGI!\\nRequest Path: \" + environ.get('PATH_INFO', '/').encode()]\n\n# Convert the WSGI app to an ASGI app\nasgi_app = ASGIMiddleware(wsgi_app)\n\n# To run this converted ASGI app using an ASGI server like Uvicorn:\n# 1. Save this code as 'app.py'\n# 2. Install uvicorn: pip install uvicorn\n# 3. Run from your terminal: uvicorn app:asgi_app --reload\n# Then open http://127.0.0.1:8000/ in your browser.\n\nprint(\"WSGI application 'wsgi_app' successfully wrapped as an ASGI application 'asgi_app'.\")\nprint(\"To serve it, use an ASGI server like Uvicorn (e.g., 'uvicorn app:asgi_app').\")\n","lang":"python","description":"This example demonstrates how to convert a standard synchronous WSGI application into an ASGI application using `ASGIMiddleware`. The resulting `asgi_app` can then be served by any ASGI-compatible web server like Uvicorn."},"warnings":[{"fix":"Ensure the WSGI application minimizes blocking I/O or offload such operations to a separate thread pool if absolute necessary. The `a2wsgi` library does not magically make a synchronous app asynchronous.","message":"WSGI applications converted to ASGI via `ASGIMiddleware` still execute synchronously. If your wrapped WSGI app performs blocking I/O operations (e.g., database calls, network requests) directly, it will block the entire ASGI event loop, negating the performance benefits of an async server.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Be mindful of request body sizes when running WSGI apps on ASGI servers. If handling very large bodies is critical, consider refactoring the application to be purely ASGI or use alternative streaming mechanisms where possible.","message":"When `ASGIMiddleware` converts an ASGI request with a large body to WSGI, it must buffer the entire request body into memory before passing it via `wsgi.input` to the WSGI application. This can lead to significant memory consumption for large file uploads or extensive POST data.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always convert the 'inner' application to the interface required by the 'outer' middleware. For example, to run an ASGI middleware around a WSGI app, first convert the WSGI app to ASGI, then wrap it with the ASGI middleware: `ASGI_Middleware(YOUR_ASGI_MIDDLEWARE(a2wsgi.ASGIMiddleware(your_wsgi_app)))`.","message":"When chaining multiple middleware, the order of `a2wsgi` conversion is crucial. You should convert the innermost application to the type expected by the next middleware in the chain. Incorrect ordering can lead to interface mismatches.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}