Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 重新加载模块和从属顶级模块_Python_Python 3.x_Python Internals - Fatal编程技术网

Python 重新加载模块和从属顶级模块

Python 重新加载模块和从属顶级模块,python,python-3.x,python-internals,Python,Python 3.x,Python Internals,在python中,是否有一种机制可以: 重新加载顶级模块(sys.modules[''主模块])或 只是在运行时更新顶级模块中的函数 假设我们有顶级的dsu.py,我们希望在运行时通过向导入的模块添加函数来修补导入的模块更新.simple,然后在dsu.py中通过添加对添加到更新.simple的函数的调用来修补main()。最后,我们希望重新加载更新。simple使用修补代码,然后重新加载dsu.py在更新中调用新函数。simple在信号处理程序中使用重新加载机制 dsu.py: 更新\sim

在python中,是否有一种机制可以:

  • 重新加载顶级模块(
    sys.modules[''主模块]
    )或
  • 只是在运行时更新顶级模块中的函数
  • 假设我们有顶级的
    dsu.py
    ,我们希望在运行时通过向导入的模块添加函数来修补导入的模块
    更新.simple
    ,然后在
    dsu.py
    中通过添加对添加到
    更新.simple
    的函数的调用来修补
    main()
    。最后,我们希望重新加载
    更新。simple
    使用修补代码,然后重新加载
    dsu.py
    更新中调用新函数。simple
    在信号处理程序中使用重新加载机制

    dsu.py:

    更新\simple.py:

    def computation(a,b):
        return a * b
    
    虽然这个问题与动态更新(或猴子补丁)有关,但我添加了带有补丁和测试的代码(
    python3 test.py
    ),以供参考:

    预期行为:

    $ python3 test.py 
    Starting the process: python3.4 ./dsu.py
    Result : 2
    Updating module with a patch that adds a method
    and the added method is called from the main()
    patching file updating/simple.py
    diff --git a/updating/simple.py b/updating/simple.py
    index 6bd6318..6434e5a 100644
    --- a/updating/simple.py
    +++ b/updating/simple.py
    @@ -1,2 +1,5 @@
     def computation(a,b):
         return a * b
    +
    +def anothercomputation(a,b):
    +    return a * b * b * a
    patching file ./dsu.py
    diff --git a/dsu.py b/dsu.py
    index 508f7c6..74d27e3 100644
    --- a/dsu.py
    +++ b/dsu.py
    @@ -8,6 +8,7 @@ def update_handler(sig, frame):
    
     def main():
         while True:
    +        print("Another: %i" % updating.simple.anothercomputation(2,1))
             print("Result : %i" % updating.simple.computation(1,2))
             time.sleep(0.5)
    
    diff --git a/updating/simple.py b/updating/simple.py
    index 6bd6318..6434e5a 100644
    --- a/updating/simple.py
    +++ b/updating/simple.py
    @@ -1,2 +1,5 @@
     def computation(a,b):
         return a * b
    +
    +def anothercomputation(a,b):
    +    return a * b * b * a
    Trigger dynamic software update with user signal
    Another: 4
    Result : 2
    Reverting patches
    Another: 4
    Result : 2
    Trigger dynamic software update with user signal
    Result : 2
    Stopping the process with pid 11547
    Cleaning files generated by Python
    Removing __pycache__/
    Removing updating/__pycache__/
    
    git存储库中提供了我的两个试用版:

    $ git log --oneline
    7c7c828 Trial on reloading only updated main() and executing it
    2fd68d6 Study on dynamic update of a module and dependent with Python
    
    任何见解都值得赞赏,我也很好奇这是否可能与框架黑客

    $ git log --oneline
    7c7c828 Trial on reloading only updated main() and executing it
    2fd68d6 Study on dynamic update of a module and dependent with Python