而loop赢了';python多处理中的t中断

而loop赢了';python多处理中的t中断,python,while-loop,multiprocessing,Python,While Loop,Multiprocessing,因为我还没有收到反馈,所以我在这里重新概念化(并简化)了这个问题。在这个简化的例子中,我想知道如何重新启动不会在多处理队列中中断的while循环 我想使用多处理库在多个内核上运行函数 p = multiprocessing.Pool(5) # Start a pool with 5 cores analysis = p.map(function_to_call,'abcdefghij') # Call function_to_call on every c

因为我还没有收到反馈,所以我在这里重新概念化(并简化)了这个问题。在这个简化的例子中,我想知道如何重新启动不会在多处理队列中中断的while循环

我想使用
多处理
库在多个内核上运行函数

p = multiprocessing.Pool(5)                     # Start a pool with 5 cores
analysis = p.map(function_to_call,'abcdefghij') # Call function_to_call on every core 
                                                # each time with a different letter from
                                                # string 'abcdefghij' and collect results
print(analysis)

def function_to_call(arg):
    result = []
    time = timeit.default_timer()               # timeit library needed
    time_even = int(str(time)[-1])              # get last number of time
########## below can't be changed ###########
    while True:
        if (time_even % 2) == 0:                # if last number is even, do stuff
            result.append('problem')
        else:                                   # else append arg to result
            result.append(str(arg))
            break 
########## above can't be changed ###########
    print(result) 
    return(result)                              # and return result
编译脚本时,结果总是不同的。在我的情况下,终端中的输出为:

['b']
['c']
['g']
['h']
['i']
['e']  # <--- note that the process run in parallel and not serial
在我正在编写的脚本中,在某些情况下,函数_to_调用可能不会返回输出。但是,多次重新运行该函数最终将输出一个结果(我想用时间戳模拟这个结果)。因此,我想调整我的脚本,以便在函数_to_call不返回输出时使用相同的参数调用它

不幸的是,我想调用的函数需要几个小时才能结束。所以我不想强迫它在某个预设的时间值之后崩溃。
我将感谢你的每一条评论和所有建议

以指数增长的超时重新运行它:

from stopit import ThreadingTimeout
timeout = 3600 # should probably be set slightly above the expected runtime
while True:
    with ThreadingTimeout(timeout) as timeout_ctx:
        result = function_to_call()
    if timeout_ctx.state != timeout_ctx.TIMED_OUT:
        return result
    timeout *= 2
这样,你就可以确定,你平均不会做超过两倍的工作


注意:在我的示例中,我使用了stopit库,因此您无法判断函数在等待无限长的时间后是否最终会产生结果。在
while True
循环中,
time\u甚至
都不会重新计算。因此,如果它第一次不是偶数,它将永远不会是偶数,
结果列表将不断增长。@Lee:我可以告诉你!然而,我想我需要几天的时间才能做到tell@Booboo:确切地说,只有在再次调用
fuction\u to\u call
时才会重新计算。调用需要几个小时才能完成的函数与模拟循环之间存在区别。在后一种情况下,您可能已经被传递了一个信号量作为附加参数,然后在每个循环迭代中,它可以测试它并根据是否可以有条件地获取它而中断。但是对于长时间运行的函数,您可能无法控制它的工作。也许这样可以。然而,我不清楚result_或_timeout看起来如何像一个整洁库的链接,用于运行带有timeoutthx的代码来更新代码。不幸的是,我不知道什么是
Timeout
(大写字母t),错误“name'Timeout'未定义”被打印到屏幕上
from stopit import ThreadingTimeout
timeout = 3600 # should probably be set slightly above the expected runtime
while True:
    with ThreadingTimeout(timeout) as timeout_ctx:
        result = function_to_call()
    if timeout_ctx.state != timeout_ctx.TIMED_OUT:
        return result
    timeout *= 2