码迷,mamicode.com
首页 > 编程语言 > 详细

python 同步与异步性能区别

时间:2019-01-24 18:57:35      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:int   nbsp   pre   display   span   none   import   for   print   

 

import gevent
 
def task(pid):
    """
    Some non-deterministic task
    """
    gevent.sleep(0.5)
    print(Task %s done % pid)
 
def synchronous():
    for i in range(1,10):
        task(i)
 
def asynchronous():
    threads = [gevent.spawn(task, i) for i in range(10)]
    gevent.joinall(threads)
 
print(Synchronous:)
synchronous()

print(Asynchronous:)
asynchronous()

输出

Synchronous:
Task 1 done
Task 2 done
Task 3 done
Task 4 done
Task 5 done
Task 6 done
Task 7 done
Task 8 done
Task 9 done
Asynchronous:
Task 0 done
Task 1 done
Task 2 done
Task 3 done
Task 4 done
Task 5 done
Task 6 done
Task 7 done
Task 8 done
Task 9 done

 

python 同步与异步性能区别

标签:int   nbsp   pre   display   span   none   import   for   print   

原文地址:https://www.cnblogs.com/sea-stream/p/10316073.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!