Green the world :使用monkey_patch
来源:互联网 发布:搜狐自制网络剧有哪些 编辑:程序博客网 时间:2024/06/10 09:21
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的参数指定要绿化的模块。
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的参数指定要绿化的模块。
0 0
- Green the world :使用monkey_patch
- Chemical Secret——10、Green world
- Green Building on the Rise
- Green~~~
- we have the "Green Card" System.
- THE WORLD
- 关于使用 Msde Sp4 Green心得
- Z-stack3.0 Power Green的使用
- Green and native threads -- what's the difference?
- The angel in the world!!
- We Are The World
- Serialize the world?
- The World Wide Web
- Top of The World
- observe the real world
- Top of The World
- heal the world
- Technology Changes the World!
- EditText inputType
- Gtest函数说明
- jar包下载网址
- Python 文本挖掘:使用gensim进行文本相似度计算
- 论5数字的价值定位和推销策略
- Green the world :使用monkey_patch
- Java程序执行超时——Future接口介绍
- [总结]视音频编解码技术零基础学习方法
- Python 文本挖掘:数据存储和读取
- 堆的优先队列1
- 遇见C++ Lambda
- vlc命令行参数详解
- 【程序员编程艺术】第六章:求解500万以内的亲和数
- python操作Excel读写--使用xlrd