python 并发编程 协程池

 

 

协程池

from gevent.pool import  Pool

 

 

from gevent import monkey;monkey.patch_all()
import gevent
from gevent.pool import Pool
import time



def eat(name):
    print("%s:eat 1" %name)
    time.sleep(3)
    print("%s:eat 2" %name)

def play(name):
    print("%s:play 1" % name)
    time.sleep(4)
    print("%s:play 2" % name)

pool = Pool(5)

g1 = pool.spawn(eat,"mike")
g2 = pool.spawn(play,"mike")

start_time = time.time()
gevent.joinall([g1,g2])

end_time = time.time()
print(end_time-start_time)


'''
mike:eat 1
mike:play 1
mike:eat 2
mike:play 2
4.031230688095093
'''

 

上一篇:Python 之并发编程之协程


下一篇:stm32之NVIC