Skip to main content

Tremolo is a stream-oriented, asynchronous, programmable HTTP server written in pure Python. It can also serve as an ASGI server.

Project description

Tremolo

codecov Quality Gate Status

Tremolo is a stream-oriented, asynchronous, programmable HTTP server written in pure Python. It can also serve as an 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.

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

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

@app.route('/my/url/big.data')
async def my_big_data(content_type='application/octet-stream', **server):
    # buffer_size = 16384
    buffer_size = server['context'].options['buffer_size']

    with open('/my/folder/big.data', '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 and stability over features.

With only 3k lines of code, with no dependencies other than the Python Standard Library, it gives you:

Example

Here is a complete hello world example in case you missed the usual return.

from tremolo import Tremolo

app = Tremolo()

@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, behind Tremolo (ASGI server).

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

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):

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 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.

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.

app.listen(8001)
app.listen(8002)

app.run('0.0.0.0', 8000)

You can even get higher concurrency with PyPy or uvloop:

import asyncio
import uvloop
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())

Installing

python3 -m pip install --upgrade tremolo

Testing

Just run python3 alltests.py for all tests. Or individual test_*.py in the tests/ folder, for example python3 tests/test_cli.py.

If you also want measurements with coverage:

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

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tremolo-0.0.505.tar.gz (34.6 kB view details)

Uploaded Source

Built Distribution

tremolo-0.0.505-py3-none-any.whl (41.2 kB view details)

Uploaded Python 3

File details

Details for the file tremolo-0.0.505.tar.gz.

File metadata

  • Download URL: tremolo-0.0.505.tar.gz
  • Upload date:
  • Size: 34.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.9

File hashes

Hashes for tremolo-0.0.505.tar.gz
Algorithm Hash digest
SHA256 a35f9d2e3c15f55b24d918f264ebfa56f645a6f294b7bddc175554994681cc92
MD5 57b2df90f57a2410eeb7e79ed82aa77b
BLAKE2b-256 c154394d2f05d287bb186c8cd2e50596aa2f5f019c82e06ee689b024341da199

See more details on using hashes here.

File details

Details for the file tremolo-0.0.505-py3-none-any.whl.

File metadata

  • Download URL: tremolo-0.0.505-py3-none-any.whl
  • Upload date:
  • Size: 41.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.9

File hashes

Hashes for tremolo-0.0.505-py3-none-any.whl
Algorithm Hash digest
SHA256 8b268d20bef8f165c147b20c1f7687afe3956e4cff2bf5ce3c65cd2371b48cd8
MD5 8e7546bcf9f994d2b10057cbd8b4ba67
BLAKE2b-256 c13e0347c04a7efae81c4e9f56dd61d5f3e280b94e72f926e9c12ef99485edae

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page