标签:join 个数 打印 class key event 数字 highlight patch
import gevent
from gevent import monkey
monkey.patch_all()
import time
def func(n):
time.sleep(3)
print(n)
st_time = time.time()
gevent.joinall([
gevent.spawn(func, 1),
gevent.spawn(func, 2),
gevent.spawn(func, 3),
gevent.spawn(func, 4),
gevent.spawn(func, 5),
gevent.spawn(func, 6),
gevent.spawn(func, 7),
])
end_time = time.time()
print("总耗时:", end_time - st_time)
‘‘‘
1
2
3
4
5
6
7
总耗时: 3.0030035972595215
‘‘‘
import gevent
from gevent import monkey
from gevent.pool import Pool
monkey.patch_all()
import time
def func(n):
time.sleep(3)
print(n)
pool = Pool(10)
st_time = time.time()
gevent.joinall([pool.spawn(func, n) for n in range(50)])
end_time = time.time()
print("总耗时:", end_time - st_time)
‘‘‘
0
1
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
总耗时: 15.011604309082031
‘‘‘
# 由于池子最多可以容纳10个协程,可以看到每隔三秒打印10个数字,因此总用时15秒
标签:join 个数 打印 class key event 数字 highlight patch
原文地址:https://www.cnblogs.com/traditional/p/9256617.html