3 ms·
Just the other day I had to make a choice between using threads and event loops for an IO bound workload (in Python). The constraints were that I already had a
by IgorPartola 16y ago
Just the other day I had to make a choice between using threads and event loops for an IO bound workload (in Python). The constraints were that I already had a piece of code that implemented its own event loop and I didn't feel like re-writing it; and I needed this code to be very memory conservative. Well, I tried threads. I figured why not? They are so ridiculously easy to work with in Python. Turns out there is no problem whatsoever for my workload to run on a 1000 independent threads and the memory usage goes up by only about 20MB. The best thing about it is that I don't have to re-write my underlying library and since everything I do here is IO bound, the GIL does not bother me.
My big problem with event loops is that while your framework may be all asynchronous, your code will not be. Try to make a call to MySQL and you will block the entire process. On the other hand, in this case your thread will just be put in a sleep mode and another thread will take over. Yes, there is overhead, but depending what you are doing, threads can be a great tool.