a2wsgi


Namea2wsgi JSON
Version 1.10.7 PyPI version JSON
download
home_pagehttps://github.com/abersheeran/a2wsgi
SummaryConvert WSGI app to ASGI app or ASGI app to WSGI app.
upload_time2024-07-26 14:02:22
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8.0
licenseApache-2.0
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            # a2wsgi

Convert WSGI app to ASGI app or ASGI app to WSGI app.

Pure Python. Only depend on the standard library.

Compared with other converters, the advantage is that a2wsgi will not accumulate the requested content or response content in the memory, so you don't have to worry about the memory limit caused by a2wsgi. This problem exists in converters implemented by uvicorn/startlette or hypercorn.

## Install

```
pip install a2wsgi
```

## How to use

### `WSGIMiddleware`

Convert WSGI app to ASGI app:

```python
from a2wsgi import WSGIMiddleware

ASGI_APP = WSGIMiddleware(WSGI_APP)
```

WSGIMiddleware executes WSGI applications with a thread pool of up to 10 threads by default. If you want to increase or decrease this number, just like `WSGIMiddleware(..., workers=15)`.

WSGIMiddleware utilizes a queue to direct traffic from the WSGI App to the client. To adjust the queue size, simply specify the send_queue_size parameter (default to `10`) during initialization, like so: WSGIMiddleware(..., send_queue_size=15). This enable developers to balance memory usage and application responsiveness.

### `ASGIMiddleware`

Convert ASGI app to WSGI app:

```python
from a2wsgi import ASGIMiddleware

WSGI_APP = ASGIMiddleware(ASGI_APP)
```

`ASGIMiddleware` will wait for the ASGI application's Background Task to complete before returning the last null byte. But sometimes you may not want to wait indefinitely for the execution of the Background Task of the ASGI application, then you only need to give the parameter `ASGIMiddleware(..., wait_time=5.0)`, after the time exceeds, the ASGI task corresponding to the request will be tried to cancel, and the last null byte will be returned.

You can also specify your own event loop through the `loop` parameter instead of the default event loop. Like `ASGIMiddleware(..., loop=faster_loop)`

### Access the original `Scope`/`Environ`

Sometimes you may need to access the original WSGI Environ in the ASGI application, just use `scope["wsgi_environ"]`; it is also easy to access the ASGI Scope in the WSGI Application, use `environ["asgi.scope"]`.

## Benchmark

Run `pytest ./benchmark.py -s` to compare the performance of `a2wsgi` and `uvicorn.middleware.wsgi.WSGIMiddleware` / `asgiref.wsgi.WsgiToAsgi`.

## Why a2wsgi

### Convert WSGI app to ASGI app

You can convert an existing WSGI project to an ASGI project to make it easier to migrate from WSGI applications to ASGI applications.

### Convert ASGI app to WSGI app

There is a lot of support for WSGI. Converting ASGI to WSGI, you will be able to use many existing services to deploy ASGI applications.

## Compatibility list

This list quickly demonstrates the compatibility of some common frameworks for users who are unfamiliar with the WSGI and ASGI protocols.

- WSGI: [Django(wsgi)](https://docs.djangoproject.com/en/3.0/howto/deployment/wsgi/)/[Kuí(wsgi)](https://kui.aber.sh/wsgi/)/[Pyramid](https://trypyramid.com/)/[Bottle](https://bottlepy.org/)/[Flask](https://flask.palletsprojects.com/)
- ASGI: [Django(asgi)](https://docs.djangoproject.com/en/3.0/howto/deployment/asgi/)/[Kuí(asgi)](https://kui.aber.sh/asgi/)/[Starlette](https://www.starlette.io/)/[FastAPI](https://fastapi.tiangolo.com/)/[Sanic](https://sanic.readthedocs.io/en/stable/)/[Quart](https://pgjones.gitlab.io/quart/)
- **Unsupport**: [aiohttp](https://docs.aiohttp.org/en/stable/)

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/abersheeran/a2wsgi",
    "name": "a2wsgi",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8.0",
    "maintainer_email": null,
    "keywords": null,
    "author": null,
    "author_email": "abersheeran <me@abersheeran.com>",
    "download_url": "https://files.pythonhosted.org/packages/99/06/57b4b9ae52f041cbd2467060fcc103a3150848ada1ea7e41b2da396b259a/a2wsgi-1.10.7.tar.gz",
    "platform": null,
    "description": "# a2wsgi\n\nConvert WSGI app to ASGI app or ASGI app to WSGI app.\n\nPure Python. Only depend on the standard library.\n\nCompared with other converters, the advantage is that a2wsgi will not accumulate the requested content or response content in the memory, so you don't have to worry about the memory limit caused by a2wsgi. This problem exists in converters implemented by uvicorn/startlette or hypercorn.\n\n## Install\n\n```\npip install a2wsgi\n```\n\n## How to use\n\n### `WSGIMiddleware`\n\nConvert WSGI app to ASGI app:\n\n```python\nfrom a2wsgi import WSGIMiddleware\n\nASGI_APP = WSGIMiddleware(WSGI_APP)\n```\n\nWSGIMiddleware executes WSGI applications with a thread pool of up to 10 threads by default. If you want to increase or decrease this number, just like `WSGIMiddleware(..., workers=15)`.\n\nWSGIMiddleware utilizes a queue to direct traffic from the WSGI App to the client. To adjust the queue size, simply specify the send_queue_size parameter (default to `10`) during initialization, like so: WSGIMiddleware(..., send_queue_size=15). This enable developers to balance memory usage and application responsiveness.\n\n### `ASGIMiddleware`\n\nConvert ASGI app to WSGI app:\n\n```python\nfrom a2wsgi import ASGIMiddleware\n\nWSGI_APP = ASGIMiddleware(ASGI_APP)\n```\n\n`ASGIMiddleware` will wait for the ASGI application's Background Task to complete before returning the last null byte. But sometimes you may not want to wait indefinitely for the execution of the Background Task of the ASGI application, then you only need to give the parameter `ASGIMiddleware(..., wait_time=5.0)`, after the time exceeds, the ASGI task corresponding to the request will be tried to cancel, and the last null byte will be returned.\n\nYou can also specify your own event loop through the `loop` parameter instead of the default event loop. Like `ASGIMiddleware(..., loop=faster_loop)`\n\n### Access the original `Scope`/`Environ`\n\nSometimes you may need to access the original WSGI Environ in the ASGI application, just use `scope[\"wsgi_environ\"]`; it is also easy to access the ASGI Scope in the WSGI Application, use `environ[\"asgi.scope\"]`.\n\n## Benchmark\n\nRun `pytest ./benchmark.py -s` to compare the performance of `a2wsgi` and `uvicorn.middleware.wsgi.WSGIMiddleware` / `asgiref.wsgi.WsgiToAsgi`.\n\n## Why a2wsgi\n\n### Convert WSGI app to ASGI app\n\nYou can convert an existing WSGI project to an ASGI project to make it easier to migrate from WSGI applications to ASGI applications.\n\n### Convert ASGI app to WSGI app\n\nThere is a lot of support for WSGI. Converting ASGI to WSGI, you will be able to use many existing services to deploy ASGI applications.\n\n## Compatibility list\n\nThis list quickly demonstrates the compatibility of some common frameworks for users who are unfamiliar with the WSGI and ASGI protocols.\n\n- WSGI: [Django(wsgi)](https://docs.djangoproject.com/en/3.0/howto/deployment/wsgi/)/[Ku\u00ed(wsgi)](https://kui.aber.sh/wsgi/)/[Pyramid](https://trypyramid.com/)/[Bottle](https://bottlepy.org/)/[Flask](https://flask.palletsprojects.com/)\n- ASGI: [Django(asgi)](https://docs.djangoproject.com/en/3.0/howto/deployment/asgi/)/[Ku\u00ed(asgi)](https://kui.aber.sh/asgi/)/[Starlette](https://www.starlette.io/)/[FastAPI](https://fastapi.tiangolo.com/)/[Sanic](https://sanic.readthedocs.io/en/stable/)/[Quart](https://pgjones.gitlab.io/quart/)\n- **Unsupport**: [aiohttp](https://docs.aiohttp.org/en/stable/)\n",
    "bugtrack_url": null,
    "license": "Apache-2.0",
    "summary": "Convert WSGI app to ASGI app or ASGI app to WSGI app.",
    "version": "1.10.7",
    "project_urls": {
        "Homepage": "https://github.com/abersheeran/a2wsgi",
        "Repository": "https://github.com/abersheeran/a2wsgi"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9c7ebfb542d49d7e182ce455620e8f26574fec3df50bc2e3aaa1e976eb1b9c3a",
                "md5": "d897167a636cdea0eaf382e5c18dc32e",
                "sha256": "6d7c602fb1f9cc6afc6c6d0558d3354f3c7aa281e73e6dc9e001dbfc1d9e80cf"
            },
            "downloads": -1,
            "filename": "a2wsgi-1.10.7-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "d897167a636cdea0eaf382e5c18dc32e",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8.0",
            "size": 17031,
            "upload_time": "2024-07-26T14:02:20",
            "upload_time_iso_8601": "2024-07-26T14:02:20.801048Z",
            "url": "https://files.pythonhosted.org/packages/9c/7e/bfb542d49d7e182ce455620e8f26574fec3df50bc2e3aaa1e976eb1b9c3a/a2wsgi-1.10.7-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "990657b4b9ae52f041cbd2467060fcc103a3150848ada1ea7e41b2da396b259a",
                "md5": "7398c7f713bb1494850597761de3a9ec",
                "sha256": "ce462ff7e1daac0bc57183c6f800f09a71c2a7a98ddd5cdeca149e3eabf3338e"
            },
            "downloads": -1,
            "filename": "a2wsgi-1.10.7.tar.gz",
            "has_sig": false,
            "md5_digest": "7398c7f713bb1494850597761de3a9ec",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8.0",
            "size": 18669,
            "upload_time": "2024-07-26T14:02:22",
            "upload_time_iso_8601": "2024-07-26T14:02:22.024049Z",
            "url": "https://files.pythonhosted.org/packages/99/06/57b4b9ae52f041cbd2467060fcc103a3150848ada1ea7e41b2da396b259a/a2wsgi-1.10.7.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-07-26 14:02:22",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "abersheeran",
    "github_project": "a2wsgi",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "lcname": "a2wsgi"
}
        
Elapsed time: 0.31490s