码迷,mamicode.com
首页 > 编程语言 > 详细

Python-greenlet完成多任务(代替yield),使用gevent完成多任务(协程)

时间:2018-11-08 20:49:11      阅读:325      评论:0      收藏:0      [点我收藏+]

标签:strong   ini   实现   from   mon   div   port   col   自己   

技术分享图片

 gevent 实现多任务:

import gevent

# 遇到延时就切换任务
def f1(n):
    for i in range(n):
        print(gevent.getcurrent(), i)
        gevent.sleep(0.5) # 必须用自己的延时,不能用time模块的延时
def f2(n):
    for i in range(n):
        print(gevent.getcurrent(), i)
        gevent.sleep(0.5)

def f3(n):
    for i in range(n):
        print(gevent.getcurrent(), i)
        gevent.sleep(0.5)

g1 = gevent.spawn(f1, 5) # grenlet 对象
g2 = gevent.spawn(f2, 5)
g3 = gevent.spawn(f3, 5)
g1.join() # 堵塞,+ 延时,切换
g2.join()
g3.join()

 

  结果:

 1 D:\python3.7\python.exe E:/code_pycharm/test_in_class/tt13.py
 2 <Greenlet "Greenlet-0" at 0x29a36a8: f1(5)> 0
 3 <Greenlet "Greenlet-1" at 0x29a37b8: f2(5)> 0
 4 <Greenlet "Greenlet-2" at 0x29a38c8: f3(5)> 0
 5 <Greenlet "Greenlet-0" at 0x29a36a8: f1(5)> 1
 6 <Greenlet "Greenlet-1" at 0x29a37b8: f2(5)> 1
 7 <Greenlet "Greenlet-2" at 0x29a38c8: f3(5)> 1
 8 <Greenlet "Greenlet-0" at 0x29a36a8: f1(5)> 2
 9 <Greenlet "Greenlet-1" at 0x29a37b8: f2(5)> 2
10 <Greenlet "Greenlet-2" at 0x29a38c8: f3(5)> 2
11 <Greenlet "Greenlet-0" at 0x29a36a8: f1(5)> 3
12 <Greenlet "Greenlet-1" at 0x29a37b8: f2(5)> 3
13 <Greenlet "Greenlet-2" at 0x29a38c8: f3(5)> 3
14 <Greenlet "Greenlet-0" at 0x29a36a8: f1(5)> 4
15 <Greenlet "Greenlet-1" at 0x29a37b8: f2(5)> 4
16 <Greenlet "Greenlet-2" at 0x29a38c8: f3(5)> 4
17 
18 Process finished with exit code 0

 

使用猴子补丁,可以不用把所有耗时的,阻塞的用gevent里的代替,该怎么写就怎么写。

如:time.sleep()的使用

 1 import gevent
 2 import time
 3 from  gevent import monkey
 4 
 5 monkey.patch_all()
 6 
 7 # 遇到延时就切换任务
 8 def f1(n):
 9     for i in range(n):
10         print(gevent.getcurrent(), i)
11         # gevent.sleep(0.5) # 必须用自己的延时,不能用time模块的延时
12         time.sleep(0.5)
13         
14 def f2(n):
15     for i in range(n):
16         print(gevent.getcurrent(), i)
17         gevent.sleep(0.5)
18 
19 def f3(n):
20     for i in range(n):
21         print(gevent.getcurrent(), i)
22         gevent.sleep(0.5)
23 
24 g1 = gevent.spawn(f1, 5) # grenlet 对象
25 g2 = gevent.spawn(f2, 5)
26 g3 = gevent.spawn(f3, 5)
27 g1.join() # 堵塞,+ 延时,切换
28 g2.join()
29 g3.join()

 

结果:

 1 D:\python3.7\python.exe E:/code_pycharm/test_in_class/tt13.py
 2 <Greenlet "Greenlet-0" at 0x3603268: f1(5)> 0
 3 <Greenlet "Greenlet-1" at 0x3603598: f2(5)> 0
 4 <Greenlet "Greenlet-2" at 0x3603488: f3(5)> 0
 5 <Greenlet "Greenlet-0" at 0x3603268: f1(5)> 1
 6 <Greenlet "Greenlet-1" at 0x3603598: f2(5)> 1
 7 <Greenlet "Greenlet-2" at 0x3603488: f3(5)> 1
 8 <Greenlet "Greenlet-0" at 0x3603268: f1(5)> 2
 9 <Greenlet "Greenlet-1" at 0x3603598: f2(5)> 2
10 <Greenlet "Greenlet-2" at 0x3603488: f3(5)> 2
11 <Greenlet "Greenlet-0" at 0x3603268: f1(5)> 3
12 <Greenlet "Greenlet-1" at 0x3603598: f2(5)> 3
13 <Greenlet "Greenlet-2" at 0x3603488: f3(5)> 3
14 <Greenlet "Greenlet-0" at 0x3603268: f1(5)> 4
15 <Greenlet "Greenlet-1" at 0x3603598: f2(5)> 4
16 <Greenlet "Greenlet-2" at 0x3603488: f3(5)> 4
17 
18 Process finished with exit code 0

 

用gevet.joinall 

 1 import gevent
 2 import time
 3 from  gevent import monkey
 4 
 5 monkey.patch_all()
 6 
 7 # 遇到延时就切换任务
 8 def f1(n):
 9     for i in range(n):
10         print(gevent.getcurrent(), i)
11         # gevent.sleep(0.5) # 必须用自己的延时,不能用time模块的延时
12         time.sleep(0.5)
13 
14 def f2(n):
15     for i in range(n):
16         print(gevent.getcurrent(), i)
17         gevent.sleep(0.5)
18 
19 def f3(n):
20     for i in range(n):
21         print(gevent.getcurrent(), i)
22         gevent.sleep(0.5)
23 
24 gevent.joinall(
25     gevent.spawn(f1, 5),
26     gevent.spawn(f2, 5),
27     gevent.spawn(f3, 5)
28 
29  )

 

 

 

 

 

 

 

 

 

Python-greenlet完成多任务(代替yield),使用gevent完成多任务(协程)

标签:strong   ini   实现   from   mon   div   port   col   自己   

原文地址:https://www.cnblogs.com/JerryZao/p/9931657.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!