简体   繁体   English

循环中的多处理,“池未运行”错误

[英]Multiprocessing in a loop, "Pool not running" error

I'm trying to run some calculation in loop, each calculation creates, uses and closes a pool.我试图在循环中运行一些计算,每个计算都会创建、使用和关闭一个池。 But the calculation only runs once and then throws an error: "Pool not running".但计算只运行一次,然后抛出错误:“池未运行”。 Of course the old one is not running, but shouldn't the new one be created?当然旧的没有运行,但新的不应该创建吗?

Below is a simplified example, similar to my code.下面是一个简化的例子,类似于我的代码。 More freakishly, in my actual code calculation runs 7 times before crashing, so I'm really confused what's the problem.更奇怪的是,在我的实际代码计算中,在崩溃之前运行了 7 次,所以我真的很困惑是什么问题。 Any advice appreciated!任何建议表示赞赏!

from pathos.multiprocessing import ProcessingPool as Pool

def add_two(number):  
    return (number + 2)

def parallel_function(numbers):
    pool = Pool(10)
    result = pool.imap(add_two, numbers)
    pool.close()
    pool.join()    
    return(result)

sets=[
    [1, 2, 3],
    [2, 3, 4],
    [3, 4, 5]
]

for one_set in sets:
    x = parallel_function(one_set)
    for i in x:
        print(i)

This is a pathos limitation which implements the Pool using the singleton pattern.这是使用单例模式实现 Pool 的pathos限制。

This is the related issue ticket .这是相关的问题票

I would recommend you to use another Pool of Workers implementation.我建议您使用另一个 Pool of Workers 实现。

The following assumes that pathos acts the same as multiprocessing.以下假设 pathos 的行为与多处理相同。 The following would be the problem if you were using multiprocessing.如果您使用多处理,以下将是问题。

The problem is that your function closes the pool before the imap is finished:问题是您的函数在 imap 完成之前关闭了池:

def parallel_function(numbers):
    pool = Pool(10)
    result = pool.imap(add_two, numbers)
    pool.close()
    pool.join()    
    return(result)

This should be written as:这应该写成:

def parallel_function(numbers):
    with Pool(10) as pool:
       yield from pool.imap(add_two, numbers)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM