实用工具状态机transitions
eventlet模块中select出现ValueError错误

eventlet绿化和patch原理

妙音 posted @ 2020年11月21日 12:09 in python with tags eventlet , 2036 阅读

说明

 
eventlet是一个必备工具,经常用,绿化原理有点忘记了,重新复习一遍.
 
 

三个主要问题

 
1. 绿化的原理是什么?
 
2. 绿化怎么管理?
 
3. 绿化怎么引入?
 

绿化原理

 
利用select/epolls/kqueue等操作系统提供的非阻塞操作,将阻塞改为非阻塞.
 

引用管理

 
eventlet在import之后,将模块中的属性绿化.
 
用一小段代码来查看看
import sys
import eventlet
# eventlet.monkey_patch()

httplib2 = eventlet.import_patched('httplib2')
print(httplib2)
print(httplib2.socket)

print("================")
for k,v in sys.modules.items():
    if "socket" in k:
        print(k, v)


# 打印内容
<module 'httplib2' from 'D:\\workspace\\venv\\xxx\\lib\\site-packages\\httplib2\\__init__.py'>
<module 'eventlet.green.socket' from 'D:\\workspace\\venv\\xxx\\lib\\site-packages\\eventlet\\green\\socket.py'>
================
_socket <module '_socket' from 'c:\\python\\python36\\DLLs\\_socket.pyd'>
socket <module 'socket' from 'c:\\python\\python36\\lib\\socket.py'>
__original_module_socket <module 'socket' from 'c:\\python\\python36\\lib\\socket.py'>

# 说明了什么?
绿化只是替换httplib2模块中的引用属性socket。并未改变sys.modules中的属性
 

引入绿化方法一:直接import

 
from eventlet.green import socket
 

引入绿化方法二:eventlet.import_patch

 
1.好处: 能绿化模块内部的系统模块
2.只能绿化os, select, socket, thread, time, psycopy, MySQLdb, bultines, subprocess
3.如果是import threading,不会自动绿化, 需要from eventlet.green import threading
 
import eventlet
httplib2 = eventlet.import_patched('httplib2')
print(httplib2)
print(httplib2.socket)

# 打印
<module 'httplib2' from 'D:\\workspace\\venv\\autumn-secs\\lib\\site-packages\\httplib2\\__init__.py'>
<module 'eventlet.green.socket' from 'D:\\workspace\\venv\\autumn-secs\\lib\\site-packages\\eventlet\\green\\socket.py'>
 

引入绿化方法三:eventlet.monkey_patch

 
1.与import_patch不同,会直接修改sys.modules
2.但是也只能绿化os,select等等几个模块,其它模块需要用直接引用绿化
 
 
 
Avatar_small
Diane Jones 说:
2020年12月01日 19:25

Find out Reliable and Legit Ways for Extra Income from Home. You are welcome to our scam free world of online earning. legit extra income from home

Avatar_small
SaaS Security 说:
2020年12月14日 15:00

I like this post,And I guess that they having fun to read this post,they shall take a good site to make a information,thanks for sharing it to me.

Avatar_small
LEGEND SEO 说:
2020年12月23日 21:36

nice post, keep up with this interesting work. It really is good to know that this topic is being covered also on this web site so cheers for taking time to discuss this! casino online canada

Avatar_small
Pak24tv 说:
2020年12月26日 13:13

I am happy to find your distinguished way of writing the post. Now you make it easy for me to understand and implement the concept. Thank you for the post. percent error calculator derivative calculator fraction calculator factorial calculator

Avatar_small
LEGEND SEO 说:
2020年12月26日 16:16

it was a wonderful chance to visit this kind of site and I am happy to know. thank you so much for giving us a chance to have this opportunity.. magyar online kaszinó

Avatar_small
LEGEND SEO 说:
2020年12月26日 20:47

I really loved reading your blog. It was very well authored and easy to undertand. Unlike additional blogs I have read which are really not tht good. I also found your posts very interesting. In fact after reading, I had to go show it to my friend and he ejoyed it as well! kasyna online

Avatar_small
LEGEND SEO 说:
2020年12月27日 17:18

I wanted to thank you for this excellent read!! I definitely loved every little bit of it. I have you bookmarked your site to check out the new stuff you post. kasyno bez depozytu

Avatar_small
LEGEND SEO 说:
2020年12月28日 16:07

Interesting post. I Have Been wondering about this issue, so thanks for posting. Pretty cool post.It 's really very nice and Useful post.Thanks woodworking business

Avatar_small
Taryn Witting 说:
2020年12月28日 23:07

TN Plus Two Question Paper 2021 A government of Tamil Nadu Directorate of Government Examination Responsible For Conducting Higher Secondary Examination (HSC) Plus two & Plus One Annual Examinations Tamil Nadu 12th Question Paper 2021 Responsible for the Government Examinations is an independent TN Plus Two class Type of Board of School Education Government of Tamil Nadu. TN Plus Two Model Paper 2021 Students Download TN Tamil Nadu Plus Two Question Paper 2021 at Official Website at dge.tn.gov.in The department is responsible for conducting the 12th Public Examinations and a number of minor examinations and Students in Pdf Format. TNDGE Question Paper 2021 for +2

Avatar_small
Digital_Zone 说:
2021年1月08日 00:06

Wonderful blog! Do you have any tips and hints for aspiring writers? Because I’m going to start my website soon, but I’m a little lost on everything. Many thanks! Bursa halı yıkama

Avatar_small
Anita 说:
2021年1月08日 02:31

I prefer merely excellent resources and you can see these in buy YouTube views

Avatar_small
Digital_Zone 说:
2021年1月08日 20:51

Great things you’ve always shared with us. Just keep writing this kind of posts.The time which was wasted in traveling for tuition now it can be used for studies.Thanks เมล็ดกาแฟคั่ว

Avatar_small
Digital_Zone 说:
2021年1月12日 04:23

You have performed a great job on this article. It’s very precise and highly qualitative. You have even managed to make it readable and easy to read. You have some real writing talent. Thank you so much. Test Bank for Biology Today and Tomorrow With Physiology 6th Edition by Starr

Avatar_small
Imamia Quran Academy 说:
2023年8月01日 06:15

Hi everyone, I really love your story. You have very useful information; I am from Imamia Quran Academy. Online ahle tashi Madrasa has enabled students to grasp and learn Quran knowledge via one click. It includes the basic beliefs of the Shias, the history of Islam, Nahj al-Balagha (which contains the sermons of Hazrat Ali a.s), and the prayers of other imams such as the book of supplications of Hazrat Imam Sajjad a.s, (Sahifa Sajjadiya). Join our Online Shia Quran Academy now, and Start your free 3 days Trial.

Avatar_small
lahore escorts 说:
2024年1月19日 17:52

best VIP Models in Lahore If you want this service then contact Mr. Arman (03284111461)

<a href="https://vipmidelsinlahor.blogspot.com//">sexy girls in lahore!</a>
<a href="https://vipmidelsinlahor.blogspot.com//">lahore call girls!</a>
<a href="https://vipmidelsinlahor.blogspot.com//">lahore escorts!</a>
<a href="https://vipmidelsinlahor.blogspot.com//">escorts in lahore!</a>

Avatar_small
download books 说:
2024年3月21日 22:41

I love how the library encourages lifelong learning.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter