Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gevent: is it a good practice to yield in each for-loop iteration?

I'm coming from Node.js where libraries such as https://github.com/caolan/async allow to iterate asynchronously through arrays without blocking the event loop.

am I correct that achieving the same with Gevent can be done by calling sleep(0) on each loop iteration ?

is this actually necessary for a web server while parsing db queries or the blocking time of Python code (not IO operations) is negligible ?

like image 987
Gal Ben-Haim Avatar asked Oct 16 '25 04:10

Gal Ben-Haim


1 Answers

Gevent has the gevent.idle() call just for that purpose (which seems to be undocumented: http://www.gevent.org/gevent.html#useful-general-functions).

However, if you are sure that the loop will do some time consuming CPU heavy processing, it's better to offload it to a truly parallel worker using multiprocessing or Threads, but keep in mind that you have to take extra measures to make these work with Gevent nicely.

like image 184
Erik Kaplun Avatar answered Oct 17 '25 19:10

Erik Kaplun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!