import time
import gevent
from gevent.threadpool import ThreadPool
pool = ThreadPool(3)
start = time.time()
for _ in range(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)
複製代碼
(1)第一次,3個睡1s。使用了3個線程,滿了,耗時1sbash
(2)第二次,1個睡1s。使用1個線程,耗時1sui
(3)中間切換須要0.00x秒。spa
(4)理論總共耗時(2+0.00x)秒。線程
Running "time.sleep(1)" 4 times with 3 threads. Should take about 2 seconds: 2.004s
Process finished with exit code 0
複製代碼