本文介绍了Python多进程池.当工作进程之一确定不再需要执行任何工作时,如何退出脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

mp.set_start_method('spawn')
total_count = Counter(0)
pool = mp.Pool(initializer=init, initargs=(total_count,), processes=num_proc)    

pool.map(part_crack_helper, product(seed_str, repeat=4))
pool.close()
pool.join()

所以我有一个工作人员池,可以完成一些工作.它只需要找到一个解决方案.因此,当一个工作进程找到解决方案时,我想停止一切.

So I have a pool of worker process that does some work. It just needs to find one solution. Therefore, when one of the worker processes finds the solution, I want to stop everything.

我想到的一种方法是只调用sys.exit().但是,由于其他进程正在运行,因此似乎无法正常工作.

One way I thought of was just calling sys.exit(). However, that doesn't seem like it's working properly since other processes are running.

另一种方法是检查每个进程调用的返回值(part_crack_helper函数的返回值),然后在该进程上终止调用.但是,我不知道在使用该map函数时该怎么做.

One other way was to check for the return value of each process calls (the return value of part_crack_helper function) and call terminate on that process. However, I don't know how to do that when using that map function.

我应该如何实现?

推荐答案

您可以使用Pool.apply_async中的回调.

类似的事情应该可以为您完成工作.

Something like this should do the job for you.

from multiprocessing import Pool


def part_crack_helper(args):
    solution = do_job(args)
    if solution:
        return True
    else:
        return False


class Worker():
    def __init__(self, workers, initializer, initargs):
        self.pool = Pool(processes=workers, initializer=initializer, initargs=initargs)

    def callback(self, result):
        if result:
            print "Solution found! Yay!"
            self.pool.terminate()

    def do_job(self):
        for args in product(seed_str, repeat=4):
            self.pool.apply_async(part_crack_helper, args=args, callback=self.callback)
        self.pool.close()
        self.pool.join()
        print "good bye"


w = Worker(num_proc, init, [total_count])
w.do_job()

这篇关于Python多进程池.当工作进程之一确定不再需要执行任何工作时,如何退出脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 06:26