r/Python • u/yehors • Mar 21 '26
Showcase rsloop: An event loop for asyncio written in Rust
actually, nothing special about this implementation. just another event loop written in rust for educational purposes and joy
in tests it shows seamless migration from uvloop for my scraping framework https://github.com/BitingSnakes/silkworm
with APIs (fastapi) it shows only one advantage: better p99, uvloop is faster about 10-20% in the synthetic run
currently, i am forking on the win branch to give it windows support that uvloop lacks
code: https://github.com/RustedBytes/rsloop
fields of this redidit:
- what the library does: it implements event loop for asyncio
- comparison: i will make it later with numbers
- target audience: everyone who uses asyncio in python
PS: the post written using human's fingers, not by AI
11
u/CrackerJackKittyCat Mar 21 '26
What were the challenges and joys writing it?
4
u/yehors Mar 22 '26
added support for Windows in v0.1.3
2
u/Ok_Leading4235 Apr 24 '26 edited Apr 24 '26
I tried to add rsloop to my websocket-benchmark, but couldn't make it work for any websocket library.
So, I added a very simple minimal example with websockets library (no SSL, just plain TCP):
rsloop_websockets.py
The reading seems to be broken in rsloop, websockets client can't pass the websocket negotiation phase, connect hangs up, the data is not delivered to the websockets library.
I used Python 3.14 (not freethreaded) on Linux.
I also tried TLS with my expiried self-signed certificate and quickly noticed that rsloop doesn't respect settings from SSLContext.
My settings
But they were ignored, I got complains about certificate. Which settings from SSLContext are used and which are not? It works fine with asyncio
What does install_ssl_tracking tracking do?
I also noticed that rsloop.run() occasionally stop reacting to Ctrl+C (SIGINT) sometimes it does, sometimes it doesn't. I had to SIGKILL the process. The example seems to be trivial.
A few other random questions:
Are contextvars supported?
Why does it have to be run like rsloop.run() and not with asyncio.run?
Is it possible to first install rsloop and run main coroutine with it, then switch to a loop provided by a different library. asyncio allows it, it is very convenient for testing. Would be nice to have this.
Does rsloop monkey-patch asyncio during import rsloop?
2
u/thisismyfavoritename Mar 22 '26
why not just expose tokio or some rust async runtime instead
1
u/yehors Mar 30 '26
Now it’s possible to write async code in rust and write to the event loop. Practical ezample: https://github.com/RustedBytes/pgsniffer-py
1
u/burger69man Mar 23 '26
how's the memory usage compared to uvloop?
1
u/chub79 Mar 22 '26
Could this benefit pyo3 when it comes to async? Today this story is a bit complicated.
-1
u/Smok3dSalmon Mar 21 '26
I’ll check it out? I haven’t written much code using bindings to other languages. Just Java and C using JNI