python 协程 gevent原理与用法分析
作者:小飞侠v科比 时间:2021-10-12 23:36:19
本文实例讲述了python 协程 gevent原理与用法。分享给大家供大家参考,具体如下:
gevent
greenlet已经实现了协程,但是这个还的人工切换,是不是觉得太麻烦了,不要捉急,python还有一个比greenlet更强大的并且能够自动切换任务的模块gevent
其原理是当一个greenlet遇到IO(指的是input output 输入输出,比如网络、文件操作等)操作时,比如访问网络,就自动切换到其他的greenlet,等到IO操作完成,再在适当的时候切换回来继续执行。
由于IO操作非常耗时,经常使程序处于等待状态,有了gevent为我们自动切换协程,就保证总有greenlet在运行,而不是等待IO
安装
pip3 install gevent
1. gevent的使用
import gevent
def f(n):
for i in range(n):
print(gevent.getcurrent(), i)
g1 = gevent.spawn(f, 5)
g2 = gevent.spawn(f, 5)
g3 = gevent.spawn(f, 5)
g1.join()
g2.join()
g3.join()
运行结果
<Greenlet at 0x10e49f550: f(5)> 0
<Greenlet at 0x10e49f550: f(5)> 1
<Greenlet at 0x10e49f550: f(5)> 2
<Greenlet at 0x10e49f550: f(5)> 3
<Greenlet at 0x10e49f550: f(5)> 4
<Greenlet at 0x10e49f910: f(5)> 0
<Greenlet at 0x10e49f910: f(5)> 1
<Greenlet at 0x10e49f910: f(5)> 2
<Greenlet at 0x10e49f910: f(5)> 3
<Greenlet at 0x10e49f910: f(5)> 4
<Greenlet at 0x10e49f4b0: f(5)> 0
<Greenlet at 0x10e49f4b0: f(5)> 1
<Greenlet at 0x10e49f4b0: f(5)> 2
<Greenlet at 0x10e49f4b0: f(5)> 3
<Greenlet at 0x10e49f4b0: f(5)> 4
可以看到,3个greenlet是依次运行而不是交替运行
2. gevent切换执行
import gevent
def f(n):
for i in range(n):
print(gevent.getcurrent(), i)
#用来模拟一个耗时操作,注意不是time模块中的sleep
gevent.sleep(1)
g1 = gevent.spawn(f, 5)
g2 = gevent.spawn(f, 5)
g3 = gevent.spawn(f, 5)
g1.join()
g2.join()
g3.join()
运行结果
<Greenlet at 0x7fa70ffa1c30: f(5)> 0
<Greenlet at 0x7fa70ffa1870: f(5)> 0
<Greenlet at 0x7fa70ffa1eb0: f(5)> 0
<Greenlet at 0x7fa70ffa1c30: f(5)> 1
<Greenlet at 0x7fa70ffa1870: f(5)> 1
<Greenlet at 0x7fa70ffa1eb0: f(5)> 1
<Greenlet at 0x7fa70ffa1c30: f(5)> 2
<Greenlet at 0x7fa70ffa1870: f(5)> 2
<Greenlet at 0x7fa70ffa1eb0: f(5)> 2
<Greenlet at 0x7fa70ffa1c30: f(5)> 3
<Greenlet at 0x7fa70ffa1870: f(5)> 3
<Greenlet at 0x7fa70ffa1eb0: f(5)> 3
<Greenlet at 0x7fa70ffa1c30: f(5)> 4
<Greenlet at 0x7fa70ffa1870: f(5)> 4
<Greenlet at 0x7fa70ffa1eb0: f(5)> 4
3. 给程序打补丁
from gevent import monkey
import gevent
import random
import time
def coroutine_work(coroutine_name):
for i in range(10):
print(coroutine_name, i)
time.sleep(random.random())
gevent.joinall([
gevent.spawn(coroutine_work, "work1"),
gevent.spawn(coroutine_work, "work2")
])
运行结果
work1 0
work1 1
work1 2
work1 3
work1 4
work1 5
work1 6
work1 7
work1 8
work1 9
work2 0
work2 1
work2 2
work2 3
work2 4
work2 5
work2 6
work2 7
work2 8
work2 9
希望本文所述对大家Python程序设计有所帮助。
来源:https://blog.csdn.net/kai46385076/article/details/100058854
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
LotusPhp笔记之:基于ObjectUtil组件的使用分析
用css制作星级投票评分功能
![](https://img.aspxhome.com/file/UploadPic/20071/2007131101422899s.gif)
XML入门教程:XML CDATA的作用
谷歌历年母亲节Logo一览
![](https://img.aspxhome.com/file/UploadPic/20095/11/goog_md_aus-19s.jpg)
Python使用QQ邮箱发送邮件报错smtplib.SMTPAuthenticationError
![](https://img.aspxhome.com/file/2023/6/65476_0s.jpg)
asp如何在线压缩Access数据库?
Oracle数据库由dataguard备库引起的log file sync等待问题
![](https://img.aspxhome.com/file/2023/1/63371_0s.png)
python记录程序运行时间的三种方法
网站改版常见问题答疑
Python读取csv文件实例解析
![](https://img.aspxhome.com/file/2023/8/68408_0s.png)
python time.sleep()是睡眠线程还是进程
如何使用Python对Excel表格进行拼接合并
![](https://img.aspxhome.com/file/2023/9/76039_0s.png)
php fopen()函数案例详解
如何把ACCESS转成SQL数据库
ASP实现全站的301跳转
Python的命令行参数实例详解
![](https://img.aspxhome.com/file/2023/9/70029_0s.png)