python 同步与异步性能区别
Posted F
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 同步与异步性能区别相关的知识,希望对你有一定的参考价值。
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 同步与异步性能区别的主要内容,如果未能解决你的问题,请参考以下文章