协程greenlet、gevent
greenlet
为了更好使用协程来完成多任务,python中greenlet模块对其封装,从而使得切换任务变得更加简单
安装方式
pip3 install greenlet
示例代码:
from greenlet import greenlet
import time def test1():
while True:
print("-----真-----")
gr2.switch()
time.sleep(0.5) def test2():
while True:
print("-----真-----")
gr1.switch()
time.sleep(0.5) gr1 = greenlet(test1)
gr2 = greenlet(test2) # 切换到gr1中运行
gr1.swith()
gevent
greenlet已经实现了协程,但是这个工人切换,是不是觉得太麻烦了,不要着急,python还有一个比greenlet更强大的并且能够自动切换任务的模块`gevent`
其原理是当一个greentlet遇到IO(指的是input ouput输入输出,比如网络、文件操作等)操作时,比如访问网络,就自动切换到其他的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()
运行结果
F:\python3.6\python.exe D:/pythonProjects/pynetwork/coroutine/gevent_demo.py
<Greenlet "Greenlet-0" at 0x22ef10f5c48: f(5)> 0
<Greenlet "Greenlet-0" at 0x22ef10f5c48: f(5)> 1
<Greenlet "Greenlet-0" at 0x22ef10f5c48: f(5)> 2
<Greenlet "Greenlet-0" at 0x22ef10f5c48: f(5)> 3
<Greenlet "Greenlet-0" at 0x22ef10f5c48: f(5)> 4
<Greenlet "Greenlet-1" at 0x22ef12dc048: f(5)> 0
<Greenlet "Greenlet-1" at 0x22ef12dc048: f(5)> 1
<Greenlet "Greenlet-1" at 0x22ef12dc048: f(5)> 2
<Greenlet "Greenlet-1" at 0x22ef12dc048: f(5)> 3
<Greenlet "Greenlet-1" at 0x22ef12dc048: f(5)> 4
<Greenlet "Greenlet-2" at 0x22ef12dc148: f(5)> 0
<Greenlet "Greenlet-2" at 0x22ef12dc148: f(5)> 1
<Greenlet "Greenlet-2" at 0x22ef12dc148: f(5)> 2
<Greenlet "Greenlet-2" at 0x22ef12dc148: f(5)> 3
<Greenlet "Greenlet-2" at 0x22ef12dc148: f(5)> 4 Process finished with exit code 0
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()
运行结果
F:\python3.6\python.exe D:/pythonProjects/pynetwork/coroutine/gevent_demo.py
<Greenlet "Greenlet-0" at 0x2c45e304c48: f(5)> 0
<Greenlet "Greenlet-1" at 0x2c45e4cc048: f(5)> 0
<Greenlet "Greenlet-2" at 0x2c45e4cc148: f(5)> 0
<Greenlet "Greenlet-0" at 0x2c45e304c48: f(5)> 1
<Greenlet "Greenlet-1" at 0x2c45e4cc048: f(5)> 1
<Greenlet "Greenlet-2" at 0x2c45e4cc148: f(5)> 1
<Greenlet "Greenlet-0" at 0x2c45e304c48: f(5)> 2
<Greenlet "Greenlet-1" at 0x2c45e4cc048: f(5)> 2
<Greenlet "Greenlet-2" at 0x2c45e4cc148: f(5)> 2
<Greenlet "Greenlet-0" at 0x2c45e304c48: f(5)> 3
<Greenlet "Greenlet-1" at 0x2c45e4cc048: f(5)> 3
<Greenlet "Greenlet-2" at 0x2c45e4cc148: f(5)> 3
<Greenlet "Greenlet-0" at 0x2c45e304c48: f(5)> 4
<Greenlet "Greenlet-1" at 0x2c45e4cc048: f(5)> 4
<Greenlet "Greenlet-2" at 0x2c45e4cc148: f(5)> 4 Process finished with exit code 0
3. 给程序打补丁
from gevent import monkey
import gevent
import random
import time # 有耗时操作时需要
monkey.patch_all() # 将程序中用到的耗时操作的代码,换为gevent中自己实现的模块 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")
])
运行结果
F:\python3.6\python.exe D:/pythonProjects/pynetwork/coroutine/gevent_test.py
work1 0
work2 0
work2 1
work1 1
work1 2
work1 3
work2 2
work1 4
work1 5
work2 3
work1 6
work1 7
work2 4
work2 5
work1 8
work1 9
work2 6
work2 7
work2 8
work2 9 Process finished with exit code 0
协程greenlet、gevent的更多相关文章
- python 协程 greenlet gevent
一.并发的本质 切换+保存状态 cpu正在运行一个任务,会在两种情况下切走去执行其他的任务(切换由操作系统强制控制),一种情况是该任务发生了阻塞,另外一种情况是该任务计算的时间过长时间片到了 二.协程 ...
- 线程队列 concurrent 协程 greenlet gevent
死锁问题 所谓死锁:是指两个或两个以上的进程或线程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去.此时称系统处于死锁状态或系统产生了死锁,这些永远在互相等待的进 ...
- day 34 线程队列 线程池 协程 Greenlet \Gevent 模块
1 线程的其他方法 threading.current_thread().getName() 查询当前线程对象的名字 threading.current_thread().ident ...
- 协程,greenlet原生协程库, gevent库
协程简介 协程(coroutine),又称为微线程,纤程,是一种用户级的轻量级线程.协程拥有自己的寄存器上下文和栈. 协程调度切换时,将寄存器上下文和栈保存到其他地方,在切回来时,恢复之前保存的上下文 ...
- 【python】-- 协程介绍及基本示例、协程遇到IO操作自动切换、协程(gevent)并发爬网页
协程介绍及基本示例 协程,又称微线程,纤程.英文名Coroutine.一句话说明什么是协程:协程是一种用户态的轻量级线程. 协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存到其他 ...
- 并发编程(六)——进程/线程池、协程、gevent第三方库
进程/线程池.协程.gevent第三方库 一.进程/线程池 1.进程池 (1)什么是进程池 如果需要创建的子进程数量不大,可以直接利用multiprocess中的Process来创建.但是当需要创建上 ...
- python 线程(其他方法,队列,线程池,协程 greenlet模块 gevent模块)
1.线程的其他方法 from threading import Thread,current_thread import time import threading def f1(n): time.s ...
- 协程----greenlet模块,gevent模块
1.协程初识,greenlet模块 2.gevent模块(需要pip安装) 一.协程初识,greenlet模块: 协程:是单线程下的并发,又称微线程,纤程.英文名Coroutine.一句话说明什么是线 ...
- 14 并发编程-(协程)-greenlet模块&gevent模块
1.实现多个任务之间进行切换,yield.greenlet都没有实现检测I/O,greenlet在实现多任务切换下更简单 from greenlet import greenlet def eat(n ...
随机推荐
- Teigha.NET开发入门1- Teigha介绍
对于CAD开发,无疑较强大的方式是Lisp.AutoCAD二次开发,且学习资源丰富,依靠强大的AutoCAD的环境可以干很多事,省很多力.但若要脱离AutoCAD环境,那就当属Teigha了. 名称问 ...
- shell编程学习笔记(九):Shell中的case条件判断
除了可以使用if条件判断,还可以使用case 以下蓝色字体部分为Linux命令,红色字体的内容为输出的内容: # cd /opt/scripts # vim script08.sh 开始编写scrip ...
- 设置mysql 事务锁超时时间 innodb_lock_wait_timeout
Mysql数据库采用InnoDB模式,默认参数:innodb_lock_wait_timeout设置锁等待的时间是50s,一旦数据库锁超过这个时间就会报错. mysql> SHOW GLOBAL ...
- [C#] .NET Core项目修改project.json来引用其他目录下的源码等文件的办法 & 解决多框架时 project.json 与 app.config冲突的问题
作者: zyl910 一.缘由 项目规模大了后,经常会出现源码文件分布在不同目录的情况,但.NET Core项目默认只有项目目录下的源码文件,且不支持"Add As Link"方式 ...
- PHP异步扩展Swoole笔记(1)
安装Swoole扩展 通过pecl安装, 系统中最好已经有http2依赖, 如果是Ubuntu, 可以直接通过apt安装nghttp2, 如果是Centos或者需要自己编译, 在Github下载ngh ...
- easyui combox 手动添加项
$('#comzwcf').combobox({ valueField: 'id', textField: 'text', }); $.ajax({ url: '/Provider/HandlerIr ...
- vue 实站技巧总结
多个页面都使用的到方法,放在 vue.prototype上会很方便 刚接触 vue的时候做过一件傻事,因为封装了一个异步请求接口post,放在 post.js文件里面,然后在每个需要使用异步请求的页面 ...
- MySQL入门很简单-触发器
1.触发器是由事件来触发某个操作,这些事件包括insert语句.update语句和delete语句.当数据库系统执行这些事件时,会激活触发器执行相应操作.MySQL从5.0.2开始支持触发器.使用触发 ...
- 【原创 深度学习与TensorFlow 动手实践系列 - 4】第四课:卷积神经网络 - 高级篇
[原创 深度学习与TensorFlow 动手实践系列 - 4]第四课:卷积神经网络 - 高级篇 提纲: 1. AlexNet:现代神经网络起源 2. VGG:AlexNet增强版 3. GoogleN ...
- 【iCore4 双核心板_ARM】例程三十三:SD_IAP_ARM实验——更新升级STM32
实验现象及操作说明: 1.本例程共有两个代码包,APP和IAP,IAP程序功能实现将APP程序升级至STM32中. 2.直接上电或烧写程序将执行升级的APP应用程序. 3.按下按键上电或写程序将进行升 ...