python - 多处理列表管理器

标签 python multiprocessing

我有一个函数,我希望同时传递不同的输入文件。我正在使用多处理管理器来处理输出列表,节点。我将其定义为 nodes = manager.list()

file_list = [file_1,file_2,file_3]

def function_x(file,nodes):
    nodes.extend(some_data)     
    print(type(nodes))

if __name__ == "__main__":
    manager = multiprocessing.Manager()
    nodes = manager.list()
    matches = partial(function_x,nodes)
    pool = multiprocessing.Pool(processes=multiprocessing.cpu_count())
    pool.map(function_x,file_list)

尽管将 nodes 定义为列表,但我收到以下错误:AttributeError: 'str' object has no attribute 'extend'

当我打印nodes类型时,我得到string。为什么 nodes = manager.list() 没有正确定义它?

最佳答案

问题出在matches =partial(function_x,nodes)
这里 partialnodes 替换第一个 function_x 参数(例如 file),然后您将得到文件名(字符串)第二个参数,因此错误。

所以要么交换 function_x 参数:

def function_x(nodes, filename):

或者在构造部分时使用关键字参数:

matches = partial(function_x, nodes=nodes)

关于python - 多处理列表管理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40674089/

相关文章:

python - 检查对象是否为十进制的正确方法

python - 错误: nothing to repeat at position

python - 为包含 namedtuple 的列表键入提示

python - 多处理 python-server 创建了太多的临时目录

python - 无法访问 Queue.Empty : "AttributeError: ' function' object has no attribute 'Empty' "

multithreading - 达到超时并在Rust中输出过程时终止过程

python - 从 X、Y、Z 坐标数据创建 3D 表面网格

惰性可调用的 Python 依赖注入(inject)

python - 多处理 apply_async 与多处理队列的奇怪行为

python - multiprocessing.shared_memory 是否需要锁定?