python - When to use Threadpool in Gevent -
i've noticed gevent has threadpool object. can explain me when use threadpool , when use regular pool? whats difference between gevent.threadpool , gevent.pool?
when have piece of python code take's long time run (seconds) , not cause swithing of greenlets. (no networking) other greenlets / gevent jobs 'starve' , have no computation time , application 'hangs'.
if put 'heavy' task in threadpool, ,the threaded execution make sure other greenlets not starve. believe if code spends lot of time in c library have no effect.
below example gevent examples. note example uses time.sleep blocks , not gevent.sleep.
tip: if have loop takes long time run can put in gevent.sleep(0) in loop. every loop other greenlets have chance run. gevent.sleep(0) in slow loop make sure other greenlets not starve , applications appears responsive
import time import gevent gevent.threadpool import threadpool pool = threadpool(3) start = time.time() _ in xrange(4): pool.spawn(time.sleep, 1) gevent.wait() delay = time.time() - start print 'running "time.sleep(1)" 4 times 3 threads. should take 2 seconds: %.3fs' % delay
Comments
Post a Comment