gevent的同步与异步

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倍

 

posted @ 2017-08-31 16:52  一石数字欠我15w!!!  阅读(484)  评论(0编辑  收藏  举报