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的參数指定要绿化的模块。

时间: 2024-08-27 21:04:10

Green the world :使用monkey_patch的相关文章

Python——eventlet

eventlet语境下的“绿色线程”普通线程之间的区别: 1. 绿色线程几乎没有开销,不用像保留普通线程一样保留“绿色线程”,每一个网络连接对应至少一个“绿色线程”: 2. 绿色线程需要人为的设置使其互相让渡CPU控制权,而不是抢占.绿色线程既能够共享数据结构,又不需要显式的互斥控制,因为只有当一个绿色线程让出了控制权后其他的绿色线程才能访问彼此共享的数据结构. 下图是eventlet中协程.hub.线程.进程之间的关系: ____________________________________

python monkey_patch浅析

最近做了一个neutron集成vyatta的任务,顺便认真学习下neutron的代码,头几行就看到了monkey_patch. 首先就从neutron-server的启动脚本开始: [[email protected] ~]# cat /usr/bin/neutron-server #!/usr/bin/python # PBR Generated from u'console_scripts' import sys from neutron.server import main if __na

Eclipse UML插件Green UML、AmaterasUML

一.Green UML插件 1.查看Eclipse版本 查看当前电脑上安装的Eclipse版本(Help-About Eclipse Platform),是3.3.2版本的. 2.查看相应插件版本 然后去网站http://green.sourceforge.net/builds.html上查看相对应的Green以及GEF版本: 好像是Green3.0.0以及GEF3.3 3.安装Green 3.0.0 只要在上述网站中直接点击3.0.0的链接即可.将下载到的green-3.0.0.zip解压,将

21 Guns -- Green Day

21 Guns Green Day (绿日乐队)的代表曲之一.歌曲的主题是反战,同时安慰了曾 经信任布什政府如今失望透顶的美国民众.这首歌也被电影<变形金刚2> 当作插曲. Do you know what's worth fighting for When it's not worth dying for? Does it take your breath away And you feel yourself suffocating1?Does the pain weigh out the

分形之概率学下的green tree

     今天做的是分形之随机概率,可以和以前做的那个抛色子的做法非常相似,抛色子是用随机点数控制图形,今天做的树叶图形只是用概率的做法去控制图形而已,做法是如出一辙的: //图形界面 package tree0618; import java.awt.Color; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.Graphics2D: import javax.swing.JFrame; public

UVaLive6039 Uva1668 Let&#39;s Go Green

一开始考虑所有边都是单独的一条路径 然后尽量多的合并 1 #include<cstdio> 2 #include<cstring> 3 #include<cstdlib> 4 #include<algorithm> 5 #include<iostream> 6 7 using namespace std; 8 9 void setIO(const string& s) { 10 freopen((s + ".in").

[2015-12-25]OMG美语每日笔记-Go green

坚持学习英语,OMG口语非常长不错,坚持每天整理.学英语坚持最重要,学英语坚持最重要,学英语坚持最重要说三遍! Go green  环保生活 I've been thinking about ways I cloud go green .What can I do to be more environmentally friendly? 我最近在想怎样才能过绿色生活?我应该怎么才能更环保呢? reuse 循环使用 One way I've gone green is by reusing pla

Supra Indy Ns Mid Tops Blue Lime Green Mens is now available at a reduced price in the US

an ancient empire mostly located in Mesopotamia. A recent study has shown that it was probably overpopulation and drought that led to the collapse of the Assyrian civilisation.Karkemish, which flourished along the Euphrates dates back to more than 5,

Green Thread

What is Green Thread. wiki http://en.wikipedia.org/wiki/Green_threads The Wikipedia article Green Threads explains it very well. In a nutshell, Green threads are "user-level threads". They are scheduled by an "ordinary" user-level proc