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
原文地址:https://www.cnblogs.com/sea-stream/p/10316073.html
时间: 2024-10-09 01:05:02