python的协程,monkeyPatch
monkey patch 一般指运行时候进行动态替换.
基本上我们使用gevent,会在最开头的地方加入gevent.monkey.patch_all();把标准库中的thread/socket等给替换掉.这样我们在后面使用socket的时候它会变成非阻塞的了.而我们却什么也不用做.
一个案列
from gevent import monkey; monkey.patch_all()
import gevent
from urllib import request
def run_task(url):
print("开始访问 --> %s" % url)
try:
response = request.urlopen(url)
data = response.read()
print("{} bytes received from {}".forma(len(data), url))
except Exception:
print("访问中出错了")
if __name__ == '__main__':
urls = ['https://baidu.com/', 'https://github.com','https://blog.csdn.net/', 'https://cnblogs.com/lovesKey']
# 定义协程方法
greenlets = [gevent.spawn(run_task, url) for url in urls]
# 添加协程任务,并且启动运行
gevent.joinall(greenlets)
最快访问结束的会在第一位,最慢的会在最后一位.
输出结果:
Visit --> https://baidu.com/
Visit --> https://github.com
Visit --> https://blog.csdn.net/
Visit --> https://cnblogs.com/lovesKey
154097 bytes received from https://baidu.com/.
26813 bytes received from https://cnblogs.com/lovesKey.
155908 bytes received from https://blog.csdn.net/.
86916 bytes received from https://github.com.
python的协程,monkeyPatch的更多相关文章
- python gevent 协程
简介 没有切换开销.因为子程序切换不是线程切换,而是由程序自身控制,没有线程切换的开销,因此执行效率高, 不需要锁机制.因为只有一个线程,也不存在同时写变量冲突,在协程中控制共享资源不加锁,只需要判断 ...
- 深入理解Python中协程的应用机制: 使用纯Python来实现一个操作系统吧!!
本文参考:http://www.dabeaz.com/coroutines/ 作者:David Beazley 缘起: 本人最近在学习python的协程.偶然发现了David Beazley的co ...
- 关于Python的协程问题总结
协程其实就是可以由程序自主控制的线程 在python里主要由yield 和yield from 控制,可以通过生成者消费者例子来理解协程 利用yield from 向生成器(协程)传送数据# 传统的生 ...
- {python之协程}一 引子 二 协程介绍 三 Greenlet 四 Gevent介绍 五 Gevent之同步与异步 六 Gevent之应用举例一 七 Gevent之应用举例二
python之协程 阅读目录 一 引子 二 协程介绍 三 Greenlet 四 Gevent介绍 五 Gevent之同步与异步 六 Gevent之应用举例一 七 Gevent之应用举例二 一 引子 本 ...
- 【Python】协程
协程,又称微线程,纤程.英文名Coroutine. 协程的概念很早就提出来了,但直到最近几年才在某些语言(如Lua)中得到广泛应用. 子程序,或者称为函数,在所有语言中都是层级调用,比如A调用B,B在 ...
- Python之协程(coroutine)
Python之协程(coroutine) 标签(空格分隔): Python进阶 coroutine和generator的区别 generator是数据的产生者.即它pull data 通过 itera ...
- python的协程和_IO操作
协程Coroutine: 协程看上去也是子程序,但执行过程中,在子程序内部可中断,然后转而执行别的子程序,在适当的时候再返回来接着执行. 注意,在一个子程序中中断,去执行其他子程序,不是函数调用,有点 ...
- python 3 协程函数
python 3 协程函数 1:把函数的执行结果封装好__iter__和__next__,即得到一个迭代器 2:与return功能类似,都可以返回值,但不同的是,return只能返回一次值,而yiel ...
- Python之协程函数
Python之协程函数 什么是协程函数:如果一个函数内部yield的使用方法是表达式形式的话,如x=yield,那么该函数成为协程函数. def eater(name): print('%s star ...
随机推荐
- harbor上传镜像
在harbor服务器 1. 下载测试上传使用的镜像docker pull hello-world2. 打tagdocker tag docker.io/hello-world:latest 172.1 ...
- 普通表分区改造_rename方式
一.需求 配合开发人员,对业务临时表进行分区改造(业务认为的临时表,只需要保留近一月数据,并非oracle临时表类型) 二.如下记录完整过程 开发需求 TS_PM 以time_key分区 .沟通明确方 ...
- JS基础_构造函数修改
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- springboot启动流程(四)application配置文件加载过程
所有文章 https://www.cnblogs.com/lay2017/p/11478237.html 触发监听器加载配置文件 在上一篇文章中,我们看到了Environment对象的创建方法.同时也 ...
- springboot启动流程(五)创建ApplicationContext
所有文章 https://www.cnblogs.com/lay2017/p/11478237.html 正文 springboot在启动过程中将会根据当前应用的类型创建对应的ApplicationC ...
- 基于【 centos7】二 || 系统时间与网络时间同步
# date // 查看系统时间 #hwclock // 查看硬件时间 # yum -y install ntp ntpdate 安装ntpdate工具 # ntpdate cn.pool.ntp.o ...
- 封装jquery的ajax
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- dockerfile构建nginx
mkdir docker_demo cd docker_demo wget http://nginx.org/download/nginx-1.2.9.tar.gz vim Dockerfile FR ...
- mac使用sourcetree跳过注册
转自https://blog.csdn.net/qq_32890891/article/details/89216954 打开sourcetree 关闭sourcetree 命令终端输入default ...
- html和css的一些常用标签使用
HTML(HyperText Mark-up Language)超文本标签语言 <!DOCTYPE html> <!--声明这是一个html文档--> <html> ...