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

  1. Python——eventlet

    eventlet语境下的“绿色线程”普通线程之间的区别: 1. 绿色线程几乎没有开销,不用像保留普通线程一样保留“绿色线程”,每一个网络连接对应至少一个“绿色线程”: 2. 绿色线程需要人为的设置使其 ...

  2. 分形之概率学下的green tree

         今天做的是分形之随机概率,可以和以前做的那个抛色子的做法非常相似,抛色子是用随机点数控制图形,今天做的树叶图形只是用概率的做法去控制图形而已,做法是如出一辙的: //图形界面 package ...

  3. python 安装nltk,使用(英文分词处理,词干化等)(Green VPN)

    安装pip命令之后: sudo pip install -U pyyaml nltk import nltk nltk.download() 等待ing 目前访问不了,故使用Green VPN htt ...

  4. Eclipse UML插件Green UML、AmaterasUML

    一.Green UML插件 1.查看Eclipse版本 查看当前电脑上安装的Eclipse版本(Help-About Eclipse Platform),是3.3.2版本的. 2.查看相应插件版本 然 ...

  5. elasticsearch 索引 red 状态恢复 green

    方案一 找到状态为 red 的索引 curl -X GET "http://172.xxx.xxx.174:9288/_cat/indices?v=" red open index ...

  6. 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 ...

  7. [物理学与PDEs]第5章第2节 变形的描述, 应变张量 2.2 Cauchy - Green 应变张量

    1.  引理 (极分解): 设 $|{\bf F}|\neq 0$, 则存在正交阵 ${\bf R}$ 及对称正定阵 ${\bf U},{\bf V}$ 使得 $$\bex {\bf F}={\bf ...

  8. ISP PIPLINE (附加1) Green Imbalance

    1.什么是Green imbalance 芯片的Gr和Gb通道获取的能量或者是输出的数据不一致,造成这种情况的原因一方面是Gr,Gb通道的半导体制造工艺方面的差异,另一方面是microlens的存在, ...

  9. Derive representation formula from Green’s identity

    This article introduces how to derive the representation formula used in BEM from Green's identity. ...

随机推荐

  1. 【MemSQL Start[c]UP 3.0 - Round 1 A】 Declined Finalists

    [链接]h在这里写链接 [题意] 在这里写题意 [题解] max(最大值-25,0) [错的次数] 0 [反思] 在这了写反思 [代码] #include <bits/stdc++.h> ...

  2. 使用四种框架分别实现百万websocket常连接的服务器--转

    原文地址:http://colobu.com/2015/05/22/implement-C1000K-servers-by-spray-netty-undertow-and-node-js/#Nett ...

  3. GMTC2019会后:做一场冷门的技术专场是什么体验

    上周四(6.20)GMTC2019大会的第一天,很荣幸作为「UI与图形渲染」专场出品人获得了与图形领域几位技术专家同场交流的机会. 图形技术在前端范畴内是一个相对小众的话题,虽然前端工程师几乎每天都在 ...

  4. 【习题 5-10 UVA-1597】Searching the Web

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用map < string,vector < int > >mmap[100];来记录每一个数据段某个字符串 ...

  5. NSArray NSDictionary一些用法

    //从字符串分割到数组- componentsSeparatedByString: NSString *str = [NSString alloc] initWithString:@"a,b ...

  6. widow下svn上传项目时的文件可执行权限问题

    还是项目上发现的问题,要上传Android的源码项目.这里客户端是windows的机器, 测试后发现俩个问题. 1. 文件后缀是.so的文件默认上传不了.    2. 文件后缀是.sh的文件,上传后, ...

  7. ZOJ-1649 Rescue BFS (HDU 1242)

    看题传送门: ZOJ http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649 HDU http://acm.hdu.edu. ...

  8. hdu 3306 Another kind of Fibonacci(矩阵高速幂)

    Another kind of Fibonacci                                                        Time Limit: 3000/10 ...

  9. 在ArcEngine中使用Geoprocessing工具-执行工具

    转自原文在ArcEngine中使用Geoprocessing工具-执行工具 来解析一下Geoprocessor类的Execute方法,他有两种重载,Execute(IGPProcess, ITrack ...

  10. [Angular Router] Lazy loading Module with Auxiliary router

    Found the way to handle Auxiliary router for lazy loading moudle and erge load module are different. ...