Green the world :使用monkey_patch
eventlet的monkey_patch 用于绿化一些python的模块,看看以下的样例就明确了
urls = ["http://www.haha.mx/joke/1292935",
"http://www.baidu.com"]
import eventlet
from eventlet.green import urllib2
import time
def fetch(url):
try:
print "opening0", url
time.sleep(1)
body = urllib2.urlopen(url).read()
print "done with0", url
return url, body
except urllib2.HTTPError:
return "",""
def fetch1(url):
try:
print "opening1", url
body = urllib2.urlopen(url).read()
print "done with1", url
return url, body
except urllib2.HTTPError:
return "",""
eventlet.monkey_patch()
pool = eventlet.GreenPool(100)
pool.spawn(fetch,urls[0])
pool.spawn(fetch1,urls[1])
pool.waitall()
执行结果:
opening0 http://www.haha.mx/joke/1292935
opening1 http://www.baidu.com
done with1 http://www.baidu.com
done with0 http://www.haha.mx/joke/1292935
monkey_patch函数原型:
monkey_patch(os=None, select=None, socket=None, thread=None, time=None, psycopg=None)
程序中在pool中创建两个绿色线程用于处理fetch和fetch1,fetch和fetch1不同的是fetch中调用了time.sleep(1),并且使用eventlet.monkey_patch()对time模块进行了绿化,从执行结果能够看出,先处理的处理的fetch,只是在fetch函数用调用time.sleep(1)将执行权利交给了hub,然后接着执行fetch1,假设不使用monkey_patch的话time.sleep(1)会堵塞。这就是monke_patch的奇妙之处,它在程序開始的时候调用相当于一个开关,并且能够通过monkey_patch的參数指定要绿化的模块。
Green the world :使用monkey_patch的更多相关文章
- Python——eventlet
eventlet语境下的“绿色线程”普通线程之间的区别: 1. 绿色线程几乎没有开销,不用像保留普通线程一样保留“绿色线程”,每一个网络连接对应至少一个“绿色线程”: 2. 绿色线程需要人为的设置使其 ...
- 分形之概率学下的green tree
今天做的是分形之随机概率,可以和以前做的那个抛色子的做法非常相似,抛色子是用随机点数控制图形,今天做的树叶图形只是用概率的做法去控制图形而已,做法是如出一辙的: //图形界面 package ...
- python 安装nltk,使用(英文分词处理,词干化等)(Green VPN)
安装pip命令之后: sudo pip install -U pyyaml nltk import nltk nltk.download() 等待ing 目前访问不了,故使用Green VPN htt ...
- Eclipse UML插件Green UML、AmaterasUML
一.Green UML插件 1.查看Eclipse版本 查看当前电脑上安装的Eclipse版本(Help-About Eclipse Platform),是3.3.2版本的. 2.查看相应插件版本 然 ...
- elasticsearch 索引 red 状态恢复 green
方案一 找到状态为 red 的索引 curl -X GET "http://172.xxx.xxx.174:9288/_cat/indices?v=" red open index ...
- English - Green Peanut Butter
There is a guy. He wants to drink 12 cups of green peanut butter. He needs green peanut butter. So h ...
- [物理学与PDEs]第5章第2节 变形的描述, 应变张量 2.2 Cauchy - Green 应变张量
1. 引理 (极分解): 设 $|{\bf F}|\neq 0$, 则存在正交阵 ${\bf R}$ 及对称正定阵 ${\bf U},{\bf V}$ 使得 $$\bex {\bf F}={\bf ...
- ISP PIPLINE (附加1) Green Imbalance
1.什么是Green imbalance 芯片的Gr和Gb通道获取的能量或者是输出的数据不一致,造成这种情况的原因一方面是Gr,Gb通道的半导体制造工艺方面的差异,另一方面是microlens的存在, ...
- Derive representation formula from Green’s identity
This article introduces how to derive the representation formula used in BEM from Green's identity. ...
随机推荐
- 前端开发概述+JS基础细节知识点
一 前端开发概述 html页面:html css javascript 拿到UI设计图纸:切图-->html+css静态布局-->用JS写一写动态效果-->ajax和后台进行交互,把 ...
- 格式化时间的一个好方法(补充moment)
/** * * 格式化时间 * @param {*} time * @param {*} fmt * @returns * time(new Date(), 'yyyy/MM/dd') ==> ...
- iOS:实现图片的无限轮播---之使用第三方库SDCycleScrollView
SDCycleScrollView API // // SDCycleScrollView.h // SDCycleScrollView #import <UIKit/UIKit.h> ...
- android闹钟实现原理
闹钟的原理可用下面我自己画的一幅图来概括:(不对的地方,尽管吐槽) 我们来看看新建闹钟到闹钟响铃的步骤: 1.新建一个闹钟: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...
- width:100%和width:inherit
前几天遇到过这么一个问题.我想让子盒子的宽度等于父盒子的宽度.父盒子宽度为一个具体值比如说200px.我将子盒子宽度设为了100%.按道理说应该是可以等于父盒子的宽度的,但结果并没有,而是通栏了.然后 ...
- MapReduce 图解流程
Anatomy of a MapReduce Job In MapReduce, a YARN application is called a Job. The implementation of t ...
- 【7.89%】【BNUOJ 52303】Floyd-Warshall
Time limit: 2 seconds Memory limit: 1024 megabytes In ICPCCamp, there are n cities and m (bidirectio ...
- 【27.91%】【codeforces 734E】Anton and Tree
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【u223】放牙刷
[题目链接]: [题解] 错排公式 f[n] = (n-1)*(f[n-1]+f[n-2]); 这样理解: 要从n-1和n-2递推到n; 假设第n个位置上的数要放在前n-1个位置中的k位置;则有n-1 ...
- [GraphQL] Use GraphQLList with GraphQLObject Types
When working with collections of things in GraphQL, we'll always reach out for the GraphQLListType. ...