Show HN: Bringing multithreading to Python's async event loop

github.com

32 points by nbsande 4 days ago

This project explores the integration of multithreading into the asyncio event loop in Python.

While this was initially built with enhancing CPU utilization for FastAPI servers in mind, the approach can be used with more general async programs too.

If you’re interested in diving deeper into the details, I’ve written a blog post about it here: https://www.neilbotelho.com/blog/multithreaded-async.html

noident 3 days ago

> It got me wondering if it was actually possible to make Python’s async event loop work with multiple threads.

There is built-in support for this. Take a look at loop.run_in_executor. You can await something scheduled in a separate Thread/ProcessPoolExecutor.

Granted, this is different than making the async library end-to-end multi-threaded as you seem to be trying to do, but it does seem worth mentioning in this context. You _can_ have async and multiple threads at the same time!

  • nbsande 3 days ago

    run_in_executor is pretty powerful for running sync code in async code, but my use case was more making async code utlize the cpu better. I think, just using run_in_executor would add a lot of complication and changes to how you use async await. But great point none the less!

quotemstr 3 days ago

At that point, why bother with asyncio? What we really want is something like Java virtual threads, something that doesn't have a code color.

  • btown 3 days ago

    gevent is exactly this! http://www.gevent.org/

    My startup has been using it in production for years. It excels at I/O bound workflows where you have highly concurrent real-time usage of slow/unpredictable partner APIs. You just write normal (non-async) Python code and the patched system internals create yields to the event loop whenever you’d be waiting for I/O, giving you essentially unlimited concurrency (as long as all pending requests and their context fit in RAM).

    https://github.com/gfmio/asyncio-gevent does exist to let you use asyncio code in a gevent context, but it’s far less battle-tested and we’ve avoided using it so far.

  • nbsande 3 days ago

    Hmmm. That would indeed be better. Seems like an interesting experiment to try and implement virtual threads for python!

  • drowsspa 3 days ago

    Not for those of us stuck in Java 8... Is it stable already?

    • quotemstr 3 days ago

      I hear that some Amish sects permit the use of technology that's older and proven not to be too worldly, like washing machines, chainsaws, and Java 11. Have you considered converting?

      • drowsspa a day ago

        Yeah... Unfortunately golden handcuffs are binding me to the financial sector and, specifically, to Hadoop