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 前方、有光 阅读(...) 评论(...) 编辑 收藏