Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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 Multiprocessing_Python Module - Fatal编程技术网

在多处理模块中为每个进程重新加载Python模块

在多处理模块中为每个进程重新加载Python模块,python,python-multiprocessing,python-module,Python,Python Multiprocessing,Python Module,有没有办法在使用Python的多处理模块创建的进程中加载每个进程的模块副本?我试过这个: def my_fn(process_args): import my_module my_func() …但my_模块中的子导入将被一次性加载并缓存。特别是,其中一个子导入读取一个配置文件,该文件的值是根据第一个进程的环境设置的。如果我尝试这样做: def my_fn(process_args): try: my_module = reload(my_module)

有没有办法在使用Python的多处理模块创建的进程中加载每个进程的模块副本?我试过这个:

def my_fn(process_args):
    import my_module
    my_func()
…但my_模块中的子导入将被一次性加载并缓存。特别是,其中一个子导入读取一个配置文件,该文件的值是根据第一个进程的环境设置的。如果我尝试这样做:

def my_fn(process_args):
    try:
        my_module = reload(my_module)
    except NameError:
        import my_module

…my_模块的子导入不会重新加载。

将函数放入my_模块中,例如:

def my_realod():
try:
    my_sub_module = reload(my_sub_module)
except NameError:
    import my_sub_module  
按如下方式调用
my_reload

def my_fn(process_args):
try:
    my_module = reload(my_module)
    my_module.my_reload()

except NameError:
    import my_module

您可以通过检查要重新加载的模块并重新加载它使用的任何模块来尝试实现深度重新加载功能。这不是万无一失的,例如,它无法处理以下情况:

class MyClass:
    module = import_module('amodule')
但对于你的目的来说,这已经足够好了

mymod.py

# Example submodule to re-import
print('import module mymod')

# demonstrate we can even import test as a module and it works
import sys
from test import deep_reload_module

value = 2

def a_function():
    pass

class XYZ:
    pass

class NewClass(object):
    pass
test.py

import importlib
import sys
import mymod


def deep_reload_module(name):

    mod = sys.modules.get(name)
    if not mod:
        importlib.import_module(name)
        return

    def get_mods_to_reload_recursively(name, modules_to_reload=None):
        modules_to_reload = modules_to_reload or set()
        mod = sys.modules[name]
        modules_to_reload.add(name)

        # loop through the attributes in this module and remember any
        # submodules we should also reload
        for attr in dir(mod):
            prop = getattr(mod, attr)
            if isinstance(prop, type(mymod)):
                modname = attr
            elif hasattr(prop, '__module__'):
                modname = prop.__module__
                if not modname:
                    continue
            else:
                # this thing is not a module nor does it come from another
                # module, so nothing to reimport.
                continue

            if modname in sys.builtin_module_names:
                # probably best not to reimport built-ins...
                continue

            if modname in modules_to_reload:
                # this is already marked for reimporting, so avoid infinite
                # recursion
                continue

            # get_mods_to_reload... will update modules_to_reload so no need to
            # catch the return value
            get_mods_to_reload_recursively(modname, modules_to_reload)

        return modules_to_reload

    mods_to_reload = get_mods_to_reload_recursively(name)
    for mtr in mods_to_reload:
        # best to delete everything before reloading so that you are
        # sure things get re-hooked up properly to the new modules.
        print('del sys.modules[%s]' % (mtr,))
        del sys.modules[mtr]

    importlib.import_module(name)


if __name__ == '__main__':
    orig_mymod_id = id(sys.modules['mymod'])
    deep_reload_module('mymod')
    assert orig_mymod_id != id(sys.modules['mymod'])
然后,只要在新进程启动时调用
deep\u reload\u module('module')
,甚至在每个多处理作业开始时调用更简单

NB:这在很大程度上依赖于您想要重新导入的模块之外的代码,而之前没有从该模块导入任何内容,因为如果导入了,那么该代码将继续使用旧模块或中断

例如,如果您有这样做的代码:

from module_to_reimport import a_function

但是没有明确地将
模块_保留到_reimport
任何位置,那么
函数
在模块被重新导入后被调用时可能会失败,因为它只维护对
全局()的弱引用
module\u中定义,以便重新导入
,从
sys中删除该模块后,所有这些都将消失。modules

类似主题,谢谢Xin,但该线程的答案假设您编写了相关模块。这里要处理的是一个写得很糟糕的问题——在模块中使用状态而不是类。