如果原始计算失败,是否有一种方法可以使用简单的池重新发送一段数据以进行处理?

import random
from multiprocessing import Pool

def f(x):
   if random.getrandbits(1):
       raise ValueError("Retry this computation")
   return x*x

p = Pool(5)
# If one of these f(x) calls fails, retry it with another (or same) process
p.map(f, [1,2,3])

最佳答案

如果可以(或不介意)立即重试,请使用包装该函数的装饰器:

import random
from multiprocessing import Pool
from functools import wraps

def retry(f):
    @wraps(f)
    def wrapped(*args, **kwargs):
        while True:
            try:
                return f(*args, **kwargs)
            except ValueError:
                pass
    return wrapped

@retry
def f(x):
    if random.getrandbits(1):
        raise ValueError("Retry this computation")
    return x*x

p = Pool(5)
# If one of these f(x) calls fails, retry it with another (or same) process
p.map(f, [1,2,3])

关于python多处理池重试,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11533405/

10-12 23:07