python-gevent模块(自动切换io的协程)
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
import geventdef foo(): print("Running in foo") gevent.sleep(2) print("Explicit context switch to foo again")def bar(): print("Explicit context to bar") gevent.sleep(1 ) print("Implicit context switch back to bar")def func3(): print("running func3") gevent.sleep(0)#遇到io就切换,sleep最长2秒,所以整个程序花费两秒,如果是串行需要花费3秒 print("running func3 again")gevent.joinall([ gevent.spawn(foo),#启动一个协程 gevent.spawn(bar), gevent.spawn(func3)]) |

浙公网安备 33010602011771号