7 comments

  • noident a day ago 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 16 hours ago 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 a day ago 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 a day ago 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 16 hours ago ago

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

    • drowsspa a day ago ago

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

      • quotemstr 19 hours ago 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?