Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/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 3.x_Python Multiprocessing_Python Importlib - Fatal编程技术网

Python 具有动态加载模块的多处理

Python 具有动态加载模块的多处理,python,python-3.x,python-multiprocessing,python-importlib,Python,Python 3.x,Python Multiprocessing,Python Importlib,我有一些代码是在大约15000个输入上执行的。 每个输入都包含一个目录路径,以及应该为该目录执行的python脚本的路径。例如,假设有一个脚本example.py def get_features(path): results = [] for f in os.listdir(path): if os.path.isfile(os.path.join(path,d): results.append(do_some_stuff(os.path

我有一些代码是在大约15000个输入上执行的。 每个输入都包含一个目录路径,以及应该为该目录执行的python脚本的路径。例如,假设有一个脚本example.py

def get_features(path):
    results = []
    for f in os.listdir(path):
        if os.path.isfile(os.path.join(path,d):
            results.append(do_some_stuff(os.path.join(path,f)))
    return results
可以有2到10个不同的脚本,比如example.py。。。在我的程序中,我加载以下脚本:

for script_name, script_path in scripts:
    spec = importlib.util.spec_from_file_location('{}.script'.format(script_name), script_path)
    modules[script_name] = importlib.util.module_from_specs(spec)
    spec.loader.exec_module(modules[script_name])
然后我将每个目录路径从输入匹配到元组列表中的相应脚本,最后在循环中为每个目录调用脚本

for dir_path, loaded_script in work_list:
    result = loaded_script.get_features(dir_path)
    results_set.update(result)
这个循环需要很长时间,所以我想使用一个多处理池来加快速度,比如说

def worker(path, func):
    return func(path)
p = multiprocessing.Pool(6)
all_results = p.starmap(worker, work_list)
results_set = set([item for res_list in all_results for item in res_list])
但我不能这样做,因为模块(参数列表中每个元组中的第二个对象)不可拾取,我会得到一个PicklingError。。。 我曾想过将脚本的路径而不是加载的模块传递给worker方法,但由于该方法的调用次数远大于不同脚本的数量,因此每次再次加载模块似乎非常浪费。。。 有没有一种方法可以将模块传递给辅助方法进行多处理