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 ?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With