6 ms·
I have the same question! I love Python and asynchronous stuff, and I do not know too much about threading. Is threading potentially better for IO bound tasks
by shlomo_z 2y ago
I have the same question! I love Python and asynchronous stuff, and I do not know too much about threading.
Is threading potentially better for IO bound tasks than async?
- MathMonkeyMan 2y agoPotentially, but probably not. The benefit of a parallel-enabled interpreter would be two CPU cores executing bytecode instructions at the same time in the same interpreter. So, you could have one python thread working on one set of data, and another python thread working on another set of data, and the two threads would not interfere with each other much or at all. Today, with the global interpreter lock, only one of those threads can be executing bytecode at a time.
- shlomo_z 2y ago> Today, with the global interpreter lock, only one of those threads can be executing bytecode at a time. Yes, but Python now has a version without GIL, which prompted this post in the first place. So my question is: Now, if I use a version of Python 3.13 without GIL, can a threaded Flask app do better than an AIOHTTP server.
- elashri 2y agoI think it will depend on which task we are talking about. For For CPU-bound tasks (i.e heavy computation, data processing), The No-GIL Flask with threading would likely perform better than AIOHTTP since it can truly parallelize computation across cores.Now for I/O-bound tasks (database queries, API calls or file operations) then AIOHTTP would still likely be more efficient due to its lower overhead and memory. So for your original question > Is threading potentially better for IO bound tasks than async? async will be better in general, potentially due to Async co-routines using far less memory than threads and being better under high concurrency. But that's all will depend on the details of implementation of the server.
- deleted 2y ago[deleted]
- Uptrenda 2y agoQuite honestly I'd tell you not to mix threads with asyncio. As you note: IO bound tasks aren't CPU hogs and there's little benefit to mixing it with threads. It will lead to unnecessary bugs, complexity, and problems with event loop management. Asyncio can run tens of thousands of tasks if its used properly. If you think something will block it you should check out "process pool executors." Note that its very tricky to share resources like sockets between processes so its kind of another reason to avoid stuff like this. I think Python 3.14 will have interpreter pools for even more concurrency options.
- solidasparagus 2y ago> there's little benefit to mixing it with threads > If you think something will block it you should check out "process pool executors." Note that its very tricky to share resources like sockets between processes so its kind of another reason to avoid stuff like this. Isn't that the benefit of no-gil? The ability to run CPU-intensive operations without incurring the overhead and friction of multiprocessing? Now you can do multicore processing while also having shared memory
- KaiserPro 2y ago> Is threading potentially better for IO bound tasks than async? Its a pick your poison type of deal. I _personally_ hate async. Its a suprise goto, which I don't like. However in my testing asyncio performs the same as threading. (this was with socket based IO) If you loose the GIL, then threading _should_ be much faster, as it should scale to how many cores you have. But thats only if you have enough IO to saturate a core.