Learn Python with Talk Python's 270 hours of courses

Running on Rust: Granian Web Server

Episode #463, published Sat, May 25, 2024, recorded Tue, May 7, 2024

So you've created a web app with Python using Flask, Django, FastAPI, or even Emmett. It works great on your machine. How do you get it out to the world? You'll need a production-ready web server. On this episode, we have Giovanni Barillari to tell us about his relatively-new server named Granian. It promises better performance and much better consistency than many of the more well known ones today.

Watch this episode on YouTube
Play on YouTube
Watch the live stream version

Episode Deep Dive

Guest Background:

  • Guest: Giovanni Barillari, a software engineer living in Vienna, Austria
  • Role: Site Reliability Engineer (SRE) at Sentry
  • Open Source Contributions: Previously contributed to Web2Py, creator of the Emmett web framework (formerly Web2Py), and author of the Rust-based web server Granian.

1. Emmett Web Framework

  • Origins: Emmett evolved from Giovanni’s work on Web2Py to address performance and flexibility issues, especially around synchronous vs. asynchronous code.
  • Features:
    • Batteries Included: Routing, templating, an ORM, session/auth features, and more in one cohesive framework.
    • Async Support: Emmett shifted to an async model (Python async/await) but still uses a synchronous ORM.
    • Templating System (Renoir): Designed so you can use almost pure Python for HTML templates (minimal custom syntax).
    • Where to Learn More: Emmett on GitHub.

2. Granian Web Server

  • What It Is: A production-ready HTTP server for Python applications, written entirely in Rust.
  • Supported Protocols: WSGI and ASGI (standard Python interfaces) plus a custom RSGI (Rust Server Gateway Interface) designed for Emmett but open to other frameworks.
  • Performance and Consistency:
    • Rust Event Loop: Handles network I/O outside the Python Global Interpreter Lock (GIL).
    • Latency: Emphasizes highly consistent response times, reducing the worst-case latency spikes often seen with Python-based servers.
  • Deployment Options:
    • HTTPS: Granian can handle certificates and SSL/TLS itself (or you can front it with something like Nginx or Caddy).
    • Workers & Threads: Offers multiple worker processes (each with its own Python interpreter) and different threading modes to handle Rust side concurrency.
  • Where to Learn More: Granian on GitHub.

3. RSGI vs. ASGI

  • Why RSGI?
    • More direct handling of “send” and “receive” in Rust without forcing every step to be awaited in Python.
    • Aims to remove overhead where there’s no real I/O.
    • Lets the Rust runtime handle concurrency more efficiently, especially for responses that do not require streaming.
  • ASGI Support: Granian still supports ASGI for compatibility with popular async Python frameworks (e.g., FastAPI, Starlette, Litestar).

4. Access Logs and Future Features

  • Upcoming Items: Community interest in adding an access log, maximum request thresholds for workers, and further expansions to RSGI (like built-in broadcast channels for WebSockets).
  • Focus on Open Source: Giovanni welcomes community contributions and discussions on GitHub.

Overall Takeaway

In this episode, Giovanni Barillari shares how his background in web development and site reliability led to creating a Python web framework (Emmett) and a Rust-based Python server (Granian) that address performance, consistency, and ease of deployment. Emmett focuses on “batteries-included” simplicity for Python web dev, while Granian’s Rust foundation offers more predictable latency and supports multiple Python interfaces. Both projects underscore how blending Rust for core IO and Python for application logic can yield innovative solutions in the Python web ecosystem.

Links from the show

New spaCy course: talkpython.fm

Giovanni: @gi0baro
Granian: github.com
Emmett: emmett.sh
Renoir: github.com
Watch this episode on YouTube: youtube.com
Episode transcripts: talkpython.fm

--- Stay in touch with us ---
Subscribe to Talk Python on YouTube: youtube.com
Talk Python on Bluesky: @talkpython.fm at bsky.app
Talk Python on Mastodon: talkpython
Michael on Bluesky: @mkennedy.codes at bsky.app
Michael on Mastodon: mkennedy

Talk Python's Mastodon Michael Kennedy's Mastodon