Crates.io | parviocula |
lib.rs | parviocula |
version | 0.0.3 |
source | src |
created_at | 2022-08-26 07:39:59.637798 |
updated_at | 2022-10-10 08:46:58.036833 |
description | A simple ASGI server aimed at helping the transition from python ASGI applications to an Axum application |
homepage | https://github.com/tristan/parviocula |
repository | https://github.com/tristan/parviocula |
max_upload_size | |
id | 652599 |
size | 32,932 |
A simple ASGI server aimed at helping the transition from python ASGI applications to an Axum application (or maybe in the future, any Tower based web framework).
The goal is to allow writing an Axum based application in rust, with the option of falling back onto the python ASGI application to handle requests not yet implemented in rust, allowing a slow transition from an ASGI based python service to an Axum based rust service without having to do it all in one go.
This takes on the role of the ASGI server you would currently use, using Hyper to handle all the http protocol stuff and converting the requests into ASGI messages using pyo3 and passing them onto the python ASGI application.
This requires writing a small pyo3 based wrapper for your rust server that allows launching it from python. See the asgi_only example for a minimal starting example. The README.md
in the examples details the setup for the project as well.
While in most cases you can simply use the fallback
on the Axum router to forward things not implemented in rust onto the python code, if you have some methods on the same path implemented in both rust and python (e.g. a GET handled by rust, and the POST still handled by python) you need to specificly tell the router to forward the python methods onto the ASGI router. See the mixed_routes example.
Forwarding routes from nested Axum routers onto the ASGI application will lose the path information from the parent router, and as a result the ASGI app will only have the path information from the current router passed to it. For now it's recommended to only use a single flat router, and wait until you've replaced all the ASGI parts to split the router up if you wish.
🤷. Testing on my machine with oha, comparing against uvicorn, using the asgi_example
, building with maturin develop --release
, running with oha http://localhost:3000/echo -z 10s -c 1 -H "user-agent: oha" -d "hello"
, it currently only gets ~80-90% of the requests per second of uvicorn with a single oha worker. But with 50 oha workers (oha ... -c 50
) it gets ~200% the requests per second of uvicorn. When replacing uvicorn
in a large suite of integration tests I have that take upwards of 10 minutes to complete, I see no significant difference in the completion speed of the test suite.
I've used it with Starlette directly and FastAPI. I suspect others should work as well, but I've not tested any.
Maybe? But unless you're using it to transition your code base to rust it's not worth it. It's probably slower (at least if you use uvicorn like I do) and you'll likely lose a lot of the niceties you get from your current mature ASGI server.
ServerContext
API is terrible! Can you change it?Feel free to open an Issue with suggestions or a Pull Request with changes and I'll consider it.
Again, feel free to open an Issue with suggestions or a Pull Request with changes and I'll look at changing it.
ServerContext::start
function can await
until the server actually starts listening for requestsInvalidStateError
s due to using call_soon_threadsafe
to set_result
on it's futures, and in some cases (that I haven't been able to make a minimal example for yet) the futures are beging cancelled after the call_soon_threadsafe
call but before the actual set_result
call it made. It doesn't effect anything (as the futures were cancelled), but is annoying to see the errors in the logs.