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
复制代码