I've noticed that Gevent has threadpool object. Can someone explain to me when to use threadpool and when to use regular pool? Whats the difference between gevent.threadpool and gevent.pool?
When you have a piece of python code that takes a long time to run (for seconds) and does not cause switching of greenlets, all other greenlets / gevent jobs will 'starve' and have no computation time and it will look like your application 'hangs'.
If you put this 'heavy' task in a Threadpool, the threaded execution will make sure other greenlets will not starve. But I believe if your code spends a lot of time in a C library, it will have no effect.
Below is an example from gevent. Note that the example uses time.sleep
which blocks, instead of gevent.sleep
.
TIP: If you have a loop that takes a long time to run, then you can just put in a gevent.sleep(0) in the loop. Every loop other greenlets will have a chance to run. The gevent.sleep(0) in your slow loop will make sure other greenlets will not starve and the application appears responsive
import time
import gevent
from gevent.threadpool import ThreadPool
pool = ThreadPool(3)
start = time.time()
for _ in xrange(4):
pool.spawn(time.sleep, 1)
gevent.wait()
delay = time.time() - start
print 'Running "time.sleep(1)" 4 times with 3 threads. Should take about 2 seconds: %.3fs' % delay
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