gevent的同步与异步

时间:2022-05-17 00:19:05
from gevent import spawn,joinall,monkey;monkey.patch_all()
import time
def task(pid):
time.sleep(0.5)
print('Task %s done' % pid)

def synchronous(): #普通
for i in range(10):
task(i)

def asynchronous():
g_l=[spawn(task,i) for i in range(10)] #协程
joinall(g_l)

if __name__ == '__main__':
start = time.time()
print('Synchronous:')
synchronous()
print('普通',time.time() - start)

start_vip = time.time()
print('Asynchronous:')
asynchronous()
print('vip:::',time.time() - start_vip) #速度可以快十倍

 同步执行比异步执行根据任务量可以快数倍乃至n倍