Skip to content

matthewhaynesonline/Pyper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pyper

python on a rust horse cause it's fast

For educational purposes: yet another Python web framework, but written in Rust.

chart chart

(Say the line, Bart) it's blazingly fast.

Video Guide

https://youtu.be/u8VYgITTsnw

Education vs Optimization

The implementation is meant to demonstrate concepts, not optimize the implementation.

Robyn

For a proper implementation of a Rust / Python web framework, see https://github.com/sparckles/Robyn

Build

A Makefile is provided to smooth over some of the DX. For example, triggering a maturin dev build and then running UV, etc.

You could run the maturin, uv, etc. directly, Make is just used since it's ubiquitous.

Notes (request / response flow)

Startup

  1. Python app spins up event loop uvloop
  2. Python app adds routes
  3. From the py loop, start the rust loop
  4. In the rust loop
    1. bind to a socket
    2. capture python tasks locals
      1. Need to pass these back when calling the python handlers
    3. create request handler that responds to incoming request

Runtime

  1. Incoming request handled by rust request handler
    1. Impl request incoming for hyper
  2. Send static files OR Look up route from router
  3. For dynamic route
    1. Build a request object to hand off to python (PyperRequest)
    2. Call python function from Rust to get response body (String)
  4. Take the response body string from Python and then build a response (in Rust)
  5. Send response back to client

Setup

NOTE: the project layout is non standard with the python and rust codebases being separate. This is to demonstrate using the rust code as a external dependency (even though in reality the two are tightly coupled).

See https://www.maturin.rs/project_layout.html for traditional / recommended layouts.

Deps

  1. https://www.maturin.rs/installation.html
  2. https://docs.astral.sh/uv/getting-started/
  3. https://pyo3.rs/
  4. https://github.com/PyO3/pyo3-async-runtimes
  5. https://hyper.rs/

Rust

maturin new rs
# pyo3
# also note project name difference
maturin build -r

Python

uv init
uv add uvloop
uv add --editable ../rs

Python 3.14+

https://github.com/PyO3/pyo3-async-runtimes?tab=readme-ov-file#non-standard-python-event-loops

Python allows you to use alternatives to the default asyncio event loop. One popular alternative is uvloop. In v0.13 using non-standard event loops was a bit of an ordeal, but in v0.14 it's trivial.

Resources

Benchmarking

See benchmark/README.md.

About

Pyper: Python + Hyper (rs). Bla-zin-gly fast Python web framework written in Rust.

Topics

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors