tremolo


Nametremolo JSON
Version 0.0.850 PyPI version JSON
download
home_pageNone
SummaryTremolo is a stream-oriented, asynchronous, programmable HTTP server written in pure Python. It can also serve as an ASGI server.
upload_time2024-12-20 00:05:43
maintainerNone
docs_urlNone
authorNone
requires_python>=3.6
licenseMIT License
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Tremolo
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=nggit_tremolo&metric=coverage)](https://sonarcloud.io/summary/new_code?id=nggit_tremolo)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=nggit_tremolo&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=nggit_tremolo)

Tremolo is a [stream-oriented](https://nggit.github.io/tremolo-docs/yield.html), asynchronous, programmable HTTP server written in pure Python. It can also serve as an [ASGI server](#asgi-server).

Tremolo provides a common routing functionality to some unique features such as download/upload speed limiters, etc. While maintaining its simplicity and performance.

Being built with a stream in mind, Tremolo tends to use `yield` instead of `return` in route handlers.

```python
@app.route('/hello')
async def hello_world(**server):
    yield b'Hello '
    yield b'world!'
```

You can take advantage of this to serve/generate big files efficiently:

```python
@app.route('/my/url/speedtest.bin')
async def my_big_data(**server):
    response = server['response']
    buffer_size = 16384

    response.set_content_type('application/octet-stream')

    with open('/dev/random', 'rb') as f:
        chunk = True

        while chunk:
            chunk = f.read(buffer_size)
            yield chunk
```

And other use cases…

## Features
Tremolo is only suitable for those who value [minimalism](https://en.wikipedia.org/wiki/Minimalism_%28computing%29) and stability over features.

With only **3k** lines of code, with **no dependencies** other than the [Python Standard Library](https://docs.python.org/3/library/index.html), it gives you:

* HTTP/1.x with [WebSocket support](https://nggit.github.io/tremolo-docs/websocket.html)
* Keep-Alive connections with [configurable limit](https://nggit.github.io/tremolo-docs/configuration.html#keepalive_connections)
* Stream chunked uploads
* [Stream multipart uploads](https://nggit.github.io/tremolo-docs/body.html#multipart)
* Download/upload speed throttling
* [Resumable downloads](https://nggit.github.io/tremolo-docs/resumable-downloads.html)
* Framework features; routing, middleware, etc.
* ASGI server
* PyPy compatible

## Example
Here is a complete *hello world* example in case you missed the usual `return`.

```python
from tremolo import Application

app = Application()

@app.route('/hello')
async def hello_world(**server):
    return 'Hello world!', 'latin-1'


if __name__ == '__main__':
    app.run('0.0.0.0', 8000, debug=True)
```

Well, `latin-1` on the right side is not required. The default is `utf-8`.

You can save it as `hello.py` and just run it with `python3 hello.py`.

Your first *hello world* page with Tremolo will be at http://localhost:8000/hello.

## ASGI Server
Tremolo is an HTTP Server framework. You can build abstractions on top of it, say an ASGI server.

In fact, Tremolo already has ASGI server (plus WebSocket) implementation.

So you can immediately use existing [ASGI applications / frameworks](https://asgi.readthedocs.io/en/latest/implementations.html#application-frameworks), behind Tremolo (ASGI server).

For example, If a minimal ASGI application with the name `example.py`:

```python
async def app(scope, receive, send):
    assert scope['type'] == 'http'

    await send({
        'type': 'http.response.start',
        'status': 200,
        'headers': [
            (b'content-type', b'text/plain')
        ]
    })

    await send({
        'type': 'http.response.body',
        'body': b'Hello world!'
    })
```

Then you can run as follows:

```
python3 -m tremolo --debug --bind 127.0.0.1:8000 example:app
```

To see more available options:

```
python3 -m tremolo --help
```

It's also possible to run the ASGI server programmatically ([example with uvloop](https://github.com/nggit/tremolo/blob/main/example_uvloop.py)):

```
python3 example_uvloop.py
```

## Benchmarking
The first thing to note is that Tremolo is a pure Python server framework.

As a pure Python server framework, it is hard to find a comparison.
Because most servers/frameworks today are full of steroids like `httptools`, `uvloop`, Rust, etc.

You can try comparing with [Uvicorn](https://www.uvicorn.org/) with the following option (disabling steroids to be fair):

```
uvicorn --loop asyncio --http h11 --log-level error example:app
```

vs

```
python3 -m tremolo --log-level ERROR example:app
```

You will find that Tremolo is reasonably fast.

However, it should be noted that bottlenecks often occur on the application side.
Which means that in real-world usage, throughput reflects more on the application than the server.

## Misc.
Tremolo utilizes `SO_REUSEPORT` (Linux 3.9+) to load balance worker processes.

```python
app.run('0.0.0.0', 8000, worker_num=2)
```

Tremolo can also listen to multiple ports in case you are using an external load balancer like Nginx / HAProxy.

```python
app.listen(8001)
app.listen(8002)

app.run('0.0.0.0', 8000)
```

You can even get higher concurrency with [PyPy](https://www.pypy.org/) or [uvloop](https://magic.io/blog/uvloop-blazing-fast-python-networking/):

```
python3 -m tremolo --loop uvloop --log-level ERROR example:app
```

See: [--loop](https://nggit.github.io/tremolo-docs/configuration.html#loop)

## Installing
```
python3 -m pip install --upgrade tremolo
```

## Testing
Just run `python3 alltests.py` for all tests. Or individual *test_\*.py* in the [tests/](https://github.com/nggit/tremolo/tree/main/tests) folder, for example `python3 tests/test_cli.py`.

If you also want measurements with [coverage](https://coverage.readthedocs.io/):

```
coverage run alltests.py
coverage combine
coverage report
coverage html # to generate html reports
```

## License
MIT License

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "tremolo",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": null,
    "keywords": null,
    "author": null,
    "author_email": "nggit <contact@anggit.com>",
    "download_url": "https://files.pythonhosted.org/packages/13/c0/6ed18426726a8939c91bf270d849d955106ef48d1751c160c3f4f1c749fc/tremolo-0.0.850.tar.gz",
    "platform": null,
    "description": "# Tremolo\n[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=nggit_tremolo&metric=coverage)](https://sonarcloud.io/summary/new_code?id=nggit_tremolo)\n[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=nggit_tremolo&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=nggit_tremolo)\n\nTremolo is a [stream-oriented](https://nggit.github.io/tremolo-docs/yield.html), asynchronous, programmable HTTP server written in pure Python. It can also serve as an [ASGI server](#asgi-server).\n\nTremolo provides a common routing functionality to some unique features such as download/upload speed limiters, etc. While maintaining its simplicity and performance.\n\nBeing built with a stream in mind, Tremolo tends to use `yield` instead of `return` in route handlers.\n\n```python\n@app.route('/hello')\nasync def hello_world(**server):\n    yield b'Hello '\n    yield b'world!'\n```\n\nYou can take advantage of this to serve/generate big files efficiently:\n\n```python\n@app.route('/my/url/speedtest.bin')\nasync def my_big_data(**server):\n    response = server['response']\n    buffer_size = 16384\n\n    response.set_content_type('application/octet-stream')\n\n    with open('/dev/random', 'rb') as f:\n        chunk = True\n\n        while chunk:\n            chunk = f.read(buffer_size)\n            yield chunk\n```\n\nAnd other use cases\u2026\n\n## Features\nTremolo is only suitable for those who value [minimalism](https://en.wikipedia.org/wiki/Minimalism_%28computing%29) and stability over features.\n\nWith only **3k** lines of code, with **no dependencies** other than the [Python Standard Library](https://docs.python.org/3/library/index.html), it gives you:\n\n* HTTP/1.x with [WebSocket support](https://nggit.github.io/tremolo-docs/websocket.html)\n* Keep-Alive connections with [configurable limit](https://nggit.github.io/tremolo-docs/configuration.html#keepalive_connections)\n* Stream chunked uploads\n* [Stream multipart uploads](https://nggit.github.io/tremolo-docs/body.html#multipart)\n* Download/upload speed throttling\n* [Resumable downloads](https://nggit.github.io/tremolo-docs/resumable-downloads.html)\n* Framework features; routing, middleware, etc.\n* ASGI server\n* PyPy compatible\n\n## Example\nHere is a complete *hello world* example in case you missed the usual `return`.\n\n```python\nfrom tremolo import Application\n\napp = Application()\n\n@app.route('/hello')\nasync def hello_world(**server):\n    return 'Hello world!', 'latin-1'\n\n\nif __name__ == '__main__':\n    app.run('0.0.0.0', 8000, debug=True)\n```\n\nWell, `latin-1` on the right side is not required. The default is `utf-8`.\n\nYou can save it as `hello.py` and just run it with `python3 hello.py`.\n\nYour first *hello world* page with Tremolo will be at http://localhost:8000/hello.\n\n## ASGI Server\nTremolo is an HTTP Server framework. You can build abstractions on top of it, say an ASGI server.\n\nIn fact, Tremolo already has ASGI server (plus WebSocket) implementation.\n\nSo you can immediately use existing [ASGI applications / frameworks](https://asgi.readthedocs.io/en/latest/implementations.html#application-frameworks), behind Tremolo (ASGI server).\n\nFor example, If a minimal ASGI application with the name `example.py`:\n\n```python\nasync def app(scope, receive, send):\n    assert scope['type'] == 'http'\n\n    await send({\n        'type': 'http.response.start',\n        'status': 200,\n        'headers': [\n            (b'content-type', b'text/plain')\n        ]\n    })\n\n    await send({\n        'type': 'http.response.body',\n        'body': b'Hello world!'\n    })\n```\n\nThen you can run as follows:\n\n```\npython3 -m tremolo --debug --bind 127.0.0.1:8000 example:app\n```\n\nTo see more available options:\n\n```\npython3 -m tremolo --help\n```\n\nIt's also possible to run the ASGI server programmatically ([example with uvloop](https://github.com/nggit/tremolo/blob/main/example_uvloop.py)):\n\n```\npython3 example_uvloop.py\n```\n\n## Benchmarking\nThe first thing to note is that Tremolo is a pure Python server framework.\n\nAs a pure Python server framework, it is hard to find a comparison.\nBecause most servers/frameworks today are full of steroids like `httptools`, `uvloop`, Rust, etc.\n\nYou can try comparing with [Uvicorn](https://www.uvicorn.org/) with the following option (disabling steroids to be fair):\n\n```\nuvicorn --loop asyncio --http h11 --log-level error example:app\n```\n\nvs\n\n```\npython3 -m tremolo --log-level ERROR example:app\n```\n\nYou will find that Tremolo is reasonably fast.\n\nHowever, it should be noted that bottlenecks often occur on the application side.\nWhich means that in real-world usage, throughput reflects more on the application than the server.\n\n## Misc.\nTremolo utilizes `SO_REUSEPORT` (Linux 3.9+) to load balance worker processes.\n\n```python\napp.run('0.0.0.0', 8000, worker_num=2)\n```\n\nTremolo can also listen to multiple ports in case you are using an external load balancer like Nginx / HAProxy.\n\n```python\napp.listen(8001)\napp.listen(8002)\n\napp.run('0.0.0.0', 8000)\n```\n\nYou can even get higher concurrency with [PyPy](https://www.pypy.org/) or [uvloop](https://magic.io/blog/uvloop-blazing-fast-python-networking/):\n\n```\npython3 -m tremolo --loop uvloop --log-level ERROR example:app\n```\n\nSee: [--loop](https://nggit.github.io/tremolo-docs/configuration.html#loop)\n\n## Installing\n```\npython3 -m pip install --upgrade tremolo\n```\n\n## Testing\nJust run `python3 alltests.py` for all tests. Or individual *test_\\*.py* in the [tests/](https://github.com/nggit/tremolo/tree/main/tests) folder, for example `python3 tests/test_cli.py`.\n\nIf you also want measurements with [coverage](https://coverage.readthedocs.io/):\n\n```\ncoverage run alltests.py\ncoverage combine\ncoverage report\ncoverage html # to generate html reports\n```\n\n## License\nMIT License\n",
    "bugtrack_url": null,
    "license": "MIT License",
    "summary": "Tremolo is a stream-oriented, asynchronous, programmable HTTP server written in pure Python. It can also serve as an ASGI server.",
    "version": "0.0.850",
    "project_urls": {
        "Funding": "https://github.com/sponsors/nggit",
        "Homepage": "https://github.com/nggit/tremolo",
        "Source": "https://github.com/nggit/tremolo"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d5667b458c7e1b97ca72bfbfaf22a587c4e480a42160310a4f9df57e14b3b75b",
                "md5": "bdf6a0c180287e1f32bbe79d55e50e32",
                "sha256": "881c7d2514b6d1bec9b2177bf01e8548bf975787f90d439c45c61309fba938a4"
            },
            "downloads": -1,
            "filename": "tremolo-0.0.850-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "bdf6a0c180287e1f32bbe79d55e50e32",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6",
            "size": 43305,
            "upload_time": "2024-12-20T00:05:40",
            "upload_time_iso_8601": "2024-12-20T00:05:40.424967Z",
            "url": "https://files.pythonhosted.org/packages/d5/66/7b458c7e1b97ca72bfbfaf22a587c4e480a42160310a4f9df57e14b3b75b/tremolo-0.0.850-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "13c06ed18426726a8939c91bf270d849d955106ef48d1751c160c3f4f1c749fc",
                "md5": "e2bd06c92706a0da1a2b5d3dae0fefbe",
                "sha256": "5f82c233afd5938504a24e85b10a8f8835bad7ce3db9c8a6130afae60e4bf7e9"
            },
            "downloads": -1,
            "filename": "tremolo-0.0.850.tar.gz",
            "has_sig": false,
            "md5_digest": "e2bd06c92706a0da1a2b5d3dae0fefbe",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 36923,
            "upload_time": "2024-12-20T00:05:43",
            "upload_time_iso_8601": "2024-12-20T00:05:43.007835Z",
            "url": "https://files.pythonhosted.org/packages/13/c0/6ed18426726a8939c91bf270d849d955106ef48d1751c160c3f4f1c749fc/tremolo-0.0.850.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-12-20 00:05:43",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "sponsors",
    "github_project": "nggit",
    "github_not_found": true,
    "lcname": "tremolo"
}
        
Elapsed time: 0.40456s