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的更多相关文章

  1. python 协程 greenlet gevent

    一.并发的本质 切换+保存状态 cpu正在运行一个任务,会在两种情况下切走去执行其他的任务(切换由操作系统强制控制),一种情况是该任务发生了阻塞,另外一种情况是该任务计算的时间过长时间片到了 二.协程 ...

  2. 线程队列 concurrent 协程 greenlet gevent

    死锁问题 所谓死锁:是指两个或两个以上的进程或线程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去.此时称系统处于死锁状态或系统产生了死锁,这些永远在互相等待的进 ...

  3. day 34 线程队列 线程池 协程 Greenlet \Gevent 模块

    1 线程的其他方法 threading.current_thread().getName()    查询当前线程对象的名字 threading.current_thread().ident      ...

  4. 协程,greenlet原生协程库, gevent库

    协程简介 协程(coroutine),又称为微线程,纤程,是一种用户级的轻量级线程.协程拥有自己的寄存器上下文和栈. 协程调度切换时,将寄存器上下文和栈保存到其他地方,在切回来时,恢复之前保存的上下文 ...

  5. 【python】-- 协程介绍及基本示例、协程遇到IO操作自动切换、协程(gevent)并发爬网页

    协程介绍及基本示例 协程,又称微线程,纤程.英文名Coroutine.一句话说明什么是协程:协程是一种用户态的轻量级线程. 协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存到其他 ...

  6. 并发编程(六)——进程/线程池、协程、gevent第三方库

    进程/线程池.协程.gevent第三方库 一.进程/线程池 1.进程池 (1)什么是进程池 如果需要创建的子进程数量不大,可以直接利用multiprocess中的Process来创建.但是当需要创建上 ...

  7. python 线程(其他方法,队列,线程池,协程 greenlet模块 gevent模块)

    1.线程的其他方法 from threading import Thread,current_thread import time import threading def f1(n): time.s ...

  8. 协程----greenlet模块,gevent模块

    1.协程初识,greenlet模块 2.gevent模块(需要pip安装) 一.协程初识,greenlet模块: 协程:是单线程下的并发,又称微线程,纤程.英文名Coroutine.一句话说明什么是线 ...

  9. 14 并发编程-(协程)-greenlet模块&gevent模块

    1.实现多个任务之间进行切换,yield.greenlet都没有实现检测I/O,greenlet在实现多任务切换下更简单 from greenlet import greenlet def eat(n ...

随机推荐

  1. Go语言二叉树定义及遍历算法实现

    // binary_tree 二叉树 package Algorithm import ( "reflect" ) // 二叉树定义 type BinaryTree struct ...

  2. docker-mysql-cron-backup不能执行任务

    https://github.com/shiningrise/docker-mysql-cron-backup CRON_TIME=“0 18 * * * ?” 改为 CRON_TIME=0 18 * ...

  3. fastcgi php-cgi与php-fpm区别和之间的关系

    关于FastCGI.php-cgi.php-fpm的区别是什么,各自有什么用途,以及相互间的关系是什么,查阅相关资料,可谓是众说纷纭,莫衷一是: 说法一:fastcgi是一个协议,php-fpm实现了 ...

  4. 在 Windows 8、Windows 10 桌面模式下的 .NET Framework 程序中,引用 Windows.Runtime 的 API。

    参考:1.https://www.cnblogs.com/webtojs/p/9675956.html 2.http://jennal.com/2016/04/28/using-windows-run ...

  5. webview调起浏览器

    调起浏览器 url = "intent://" + url +"#Intent;scheme=http;action=android.intent.action.VIEW ...

  6. how-can-i-see-the-size-of-files-and-directories-in-linux

    https://stackoverflow.com/questions/11720079/how-can-i-see-the-size-of-files-and-directories-in-linu ...

  7. [C++]深入解析结构化异常处理(SEH)

    http://www.cppblog.com/weiym/archive/2015/02/27/209884.html 尽管以前写过一篇SEH相关的文章<关于SEH的简单总结>, 但那真的 ...

  8. PowerDesigner数据库设计PDM基于Excel的导入导出总结

    经常用到pdm来管理代码,一两张表,手写一下还凑合,一旦表多了,就慌了.于是,开始学习用vbs进行Excel的来快速导入导出操作PDM就变得很紧急了,搜罗了网络上的很多vbs脚本,各有各的优点,但对于 ...

  9. Vue:生命周期

    一.什么是vue的生命周期 Vue中的生命周期是指组件从创建到销毁的一系列过程.看下面这张官方文档的图: 从图片中可以看出Vue的整个生命周期包括8个状态,按照先后顺序分别为: beforeCreat ...

  10. MapReduce 计数器简介

    转自:http://my.oschina.net/leejun2005/blog/276891?utm_source=tuicool&utm_medium=referral 1.计数器 简介 ...