Presenting µWebSockets for Python

A seamless performance boost for Python backends

The Python scripting environment is shockingly well-designed. Almost every thing I dislike about Node.js, Python does better. For one, Python does not enforce any one particular event-loop but rather employs a plug in system where you can replace the whole thing, parts of it, or only a fraction of it.

µWebSockets for Python integrates with asyncio by replacing a small part of the event-loop; the Selector. Everything else is left as is, making a minimal change while allowing seamless integration without disturbing other asyncio projects running on the same thread.

The extension C API is much more lightweight and portable; you can easily do 125k req/sec or 155k WebSocket messages/sec on one single CPU core on a laptop. Enable pipelining and you’re in the millions — all from within Python!

Further on, PyPI the package repository is a lot more relaxed and liberal as compared to npm, allowing the author full ownership of code published while not having to agree to any indemnification clauses. I love it — it even supports uploading different distributions based on platform.

A single pip install uws and bob’s your uncle.

While some backends run directly on asyncio, others run atop WSGI — an abstraction hiding the web server from the application. This is a possible use case here; by implementing the WSGI interface one could move Python backends atop µWebSockets with little effort. Of course this would limit the gains since Flask and Django are going to bottleneck pretty quickly but there are alternatives to those.

For people who instead want to write something new or port existing projects, directly using µWebSockets and its router would give a far better outcome performance wise.

Currently the project for Python is still under heavy development, but foundations are already laid out and working perfectly. Continued work will focus on wrapping the whole interface and cleaning up for a stable release.

Please go ahead and have a spin and tell me about how it runs or doesn’t run,


Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store