【发布时间】:2018-04-25 14:54:12
【问题描述】:
刚开始在我的代码库中使用 multiprocessing 库来并行化一个简单的 for 循环,以前,在串行 for 循环中,我将导入自定义配置 .py 文件并将其传递给函数运行。
但是,我在传入要并行化的配置模块时遇到问题。
注意。我想将多个自定义 configuration.py 传递到不同的进程中。
例子:
def get_custom_config():
config_list = []
for project_config in configs:
config = importlib.import_module("config.%s.%s" % (prefix, project_config)
config_list.append(config)
return config_list
def print_config(config):
print config.something_in_config_file
if __name__ = "__main__":
config_list = get_custom_config()
pool = mp.Pool(processes=2)
pool.map(print_config, config_list)
返回:
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/pool.py", line 251, in map
return self.map_async(func, iterable, chunksize).get()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/pool.py", line 567, in get
raise self._value
cPickle.PicklingError: Can't pickle <type 'module'>: attribute lookup __builtin__.module failed
将模块传递给并行进程的最佳方式是什么?
【问题讨论】:
-
你想只使用python2.7还是Python 3也可以?如果是这样,也可以使用 Python 3 试试这个
-
@TarunLalwani 我只想在可能的情况下使用 python2.7!
标签: python-2.7 pickle python-multiprocessing