Python code hot overloaded function reload

When I pass the sea, the sea does not speak.
Little donkey ticks, Yitianjian accompanied me to the horizon.
Everyone said that because I loved Yang Guoxia, I went out on Mount Lu.
In fact, I just fell in love with the clouds and clouds on Mount Lu.
Like the fireworks of the age of sixteen.
Code hot overloading is a common requirement in a project, especially in the development of scanning services, the code of the scanning plug-in needs to be constantly modified, so how to make the plug-in code hot and heavy load, instead of modifying the code each time. It is especially important to need to restart the service. Since I recently needed to implement the hot overload requirement in a Python project, I wrote a Python version of the code hot overloaded demo for reference only.

The reload function in python

The reload function in python2 can be used directly, without the need to import third-party modules, you can use it directly:

1
Reload(module) # reload The parameters received must be imported modules.

The reload function in python3 is moved to the imp library, so you need to import it:

1
2
from imp import reload
reload(module)

demo_1

Demo1 is based on the most common requirement, that is, there are 2 files (plugin.py, scan.py) in the same directory, and the plugin.py file is called in the scan.py file.

The plugin.py file is as follows:

1
print "plugin start scan ......"

The scan.py file is as follows:

1
2
3
4
5
import time
import plugin
while 1:
reload(plugin)
time.sleep(1)

Run scan.py and manually modify the contents of the plugin.py file to see the changes in the output.

demo2

Demo2 will be a bit more complicated, that is, there are 2 files (plugin.py, scan.py) in the same directory, and the scan.py file calls the crack function in the plugin.py file.

The plugin.py file is as follows:

1
2
def crack():
print "plugin start scan ......"

The scan.py file is as follows:

1
2
3
4
5
6
import time
import plugin
while 1:
reload(plugin)
eval("plugin.crack()")
time.sleep(1)

The result is the same as demo1, which is to reload the module before calling, and then use eval to call the function of the module.

Tears

The demo3 is for more realistic needs, namely two files in different directories (./scan.py, ./plugins/plugin.py), and the scan.py file calls the crack function in the plugin.py file in the plugins directory.

The plugin.py file is as follows:

1
2
def crack():
print "plugin start scan ......"

The scan.py file is as follows:

1
2
3
4
5
6
7
import time
exec("import plugins.plugin")
while 1:
reload(eval("plugins.plugin"))
eval("plugins.plugin.crack()")
time.sleep(1)

The result is the same as demo1. It should be noted that reload does not support the overloaded module from the way plugins improt plugin, so you can use the import plugins.plugin to import modules and reload them.

本文标题:Python code hot overloaded function reload

文章作者:nmask

发布时间:2018年11月27日 - 17:11

最后更新:2019年08月16日 - 14:08

原始链接:https://thief.one/2018/11/27/1/en/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

nmask wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!

热门文章推荐: