zloop¶
An asyncio event loop with a Zig core! ⚡
Source Code: https://github.com/Kludex/zloop
zloop is an asyncio event loop whose engine is written in Zig. It is to asyncio what uvloop is: a drop-in replacement for the default event loop that you can use without changing your application code.
The difference is what's underneath. uvloop wraps libuv from Cython; zloop is a hand-written reactor in Zig - kqueue/epoll, plus an opt-in io_uring backend on Linux - bound to CPython through a thin adapter.
The key features are:
- Drop-in: it's a normal
asyncio.AbstractEventLoop. For the common server/client workloads - TCP, TLS, Unix sockets, the streams and transport APIs - your code runs unchanged. (A few rarely-used loop APIs aren't implemented yet; see Compatibility.) - Fast: the hot paths - scheduling, timers, and socket I/O - run in Zig, not Python. See Performance for benchmarks against asyncio and uvloop.
- Familiar: works the same way uvloop does, so the tools you already know (uvicorn, AnyIO, FastAPI) just pick it up.
- Tested: it passes uvicorn's entire test suite, plus its own suite at 100% coverage.
Experimental
zloop is experimental. The API and behaviour may change at any time, and it is not yet ready for production use.
Installation¶
Requirements
zloop needs CPython 3.12+ and runs on macOS / BSD (kqueue) and Linux (epoll).
Example¶
Let's create the simplest possible thing: run a coroutine on zloop.
import asyncio
import zloop
async def main():
print("Hello from a Zig event loop 👋")
asyncio.run(main(), loop_factory=zloop.new_event_loop) # (1)!
-
This is the one line that matters.
loop_factorytellsasyncio.run()which loop to build - andzloop.new_event_loopbuilds a zloop one.Everything else is plain asyncio. That's the whole point.
Run it:
That's it. The coroutine ran, but the loop driving it - the timers, the callback scheduling, the I/O polling - was all Zig. 🎉
Python 3.12+
These docs use asyncio.run(..., loop_factory=...), which needs Python
3.12+. On 3.10 / 3.11 the loop works the same - you just start it
differently; First steps shows how.
Where to go next¶
-
The 30-second tour: how to actually plug zloop into your program.
-
The most common reason to use zloop. One CLI flag.
-
How a Zig reactor becomes a Python event loop, with diagrams.
-
The benchmarks, the methodology, and the honest caveats.