python--gevent
阿新 • • 發佈:2018-07-03
print for rom 3.0 int all imp pre from
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秒
python--gevent