微探eventlet.monkey_patch

e ventlet.monkey_patch在运行时动态修改已有的代码,而不需要修改原始代码

    在eventlet.monkey_patch中支持以下几种python原生库修改

 eventlet.monkey_patch(all=True, os=False, select=False, socket=False, thread=False, time=False) 

      Globally patches certain system modules to be greenthread-friendly.The keyword arguments afford some control over which modules are patched.
If no keyword arguments are supplied, all possible modules are patched.If keywords are set to True, only the specified modules are patched. E.g.,
``monkey_patch(socket=True, select=True)`` patches only the select and socket modules. Most arguments patch the single module of the same name
(os, time, select). The exceptions are socket, which also patches the ssl module if present; and thread, which patches thread, threading, and Queue.
It's safe to call monkey_patch multiple times

    在全局中为指定的系统模块打补丁,补丁后的模块是“绿色线程友好的”,关键字参数指示哪些模块需要被打补丁,如果 all是真,那么上面所有的模块会被打补丁,忽略后面参数;否则才由相应参数控制模块的补丁。多数参数为与自己同名的模块打补丁,如os, time, select,但是 socket 参数为真时,如果 ssl 模块也存在,会同时补丁socket模块和ssl模块,类似的,thread参数为真时,会补丁thread, threading 和 Queue 模块。

在select模块中,使用补丁后:

在select模块中,不使用补丁:

在实现中,导入python原生module,获取补丁中__patched__获取需要添加到此moudle中的属性,各个patch 模块中定义__patched__属性。如果原生module中有此属性,则进行删除。如果为False,则删除python原生中的方法。

以select为例,最新版本__pathed__属性为空,即不再提供patch方法,如果为True,添加[‘select’]属性补丁,却要删除原生模块中属性,有点不合理,太过于极端:

 而在以前的老版本中,如果为False,则保留python原来的属性,但由于之前的mod.__patched__只有['select'],其他还是在使用python原生方法

 检查提交记录,在16年1月7号被修改。

猜你喜欢

转载自www.cnblogs.com/CaesarLinsa/p/9770130.html
今日推荐