A Quart extension to provide trio support.
Project description
Quart-Trio is an extension for Quart to support the Trio event loop. This is an alternative to using the asyncio event loop present in the Python standard library and supported by default in Quart.
Usage
To enable trio support, simply use the QuartTrio app class rather than the Quart app class,
from quart_trio import QuartTrio
app = QuartTrio(__name__)
@app.route('/')
async def index():
await trio.sleep(0.01)
async with trio.open_nursery as nursery:
nursery.start_soon(...)
return ...
A more concrete example of Quart Trio in usage, which also demonstrates the clarity of the Trio API is given below. This example demonstrates a simple broadcast to all chat server with a server initiated heartbeat.
app = QuartTrio(__name__)
connections = set()
async def ws_receive():
while True:
data = await websocket.receive()
for connection in connections:
await connection.send(data)
async def ws_send():
while True:
await trio.sleep(1)
await websocket.send("Heatbeat")
@app.websocket('/ws')
async def ws():
connections.add(websocket._get_current_object())
async with trio.open_nursery() as nursery:
nursery.start_soon(ws_receive)
nursery.start_soon(ws_send)
connections.remove(websocket._get_current_object())
Background Tasks
To start a task in Trio you need a nursery, for a background task you need a nursery that exists after the request has completed. In Quart-Trio this nursery exists on the app,
@app.route("/")
async def trigger_job():
app.nursery.start_soon(background_task)
return "Started", 201
MultiErrors
MultiErrors raised during the handling of a request or websocket are caught and the exceptions contianed are checked against the handlers, the first handled exception will be returned. This may lead to non-deterministic code in that it will depend on which error is raised first (in the case that multi errors can be handled).
Deployment
To run Quart-Trio in production you should use an ASGI server that supports Trio. At the moment only Hypercorn does so.
Contributing
Quart-Trio is developed on GitLab. You are very welcome to open issues or propose merge requests.
Testing
The best way to test Quart-Trio is with Tox,
$ pip install tox
$ tox
this will check the code style and run the tests.
Help
This README is the best place to start, after that try opening an issue.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for Quart_Trio-0.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d420492740ec06fd1874fb7e2d44252fd87f9050658d9b9b45106fd70136b23a |
|
MD5 | 1ce3a6042e88c07e7d13d410e90b7379 |
|
BLAKE2b-256 | 2ec43f200aacdbf0fcccc0984374d670599dd1e70cfff4dd1827662baa37aa1b |