When to use Threadpool in Gevent

假装没事ソ 提交于 2021-02-05 20:27:04

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/18411183/when-to-use-threadpool-in-gevent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!