<tfoot id='IQs3Q'></tfoot>
      <bdo id='IQs3Q'></bdo><ul id='IQs3Q'></ul>
    <legend id='IQs3Q'><style id='IQs3Q'><dir id='IQs3Q'><q id='IQs3Q'></q></dir></style></legend>

      <i id='IQs3Q'><tr id='IQs3Q'><dt id='IQs3Q'><q id='IQs3Q'><span id='IQs3Q'><b id='IQs3Q'><form id='IQs3Q'><ins id='IQs3Q'></ins><ul id='IQs3Q'></ul><sub id='IQs3Q'></sub></form><legend id='IQs3Q'></legend><bdo id='IQs3Q'><pre id='IQs3Q'><center id='IQs3Q'></center></pre></bdo></b><th id='IQs3Q'></th></span></q></dt></tr></i><div id='IQs3Q'><tfoot id='IQs3Q'></tfoot><dl id='IQs3Q'><fieldset id='IQs3Q'></fieldset></dl></div>
    1. <small id='IQs3Q'></small><noframes id='IQs3Q'>

    2. Python 多进程池.当其中一个工作进程确定不再需要

      时间:2023-05-27
            <tbody id='eYJMy'></tbody>
          • <i id='eYJMy'><tr id='eYJMy'><dt id='eYJMy'><q id='eYJMy'><span id='eYJMy'><b id='eYJMy'><form id='eYJMy'><ins id='eYJMy'></ins><ul id='eYJMy'></ul><sub id='eYJMy'></sub></form><legend id='eYJMy'></legend><bdo id='eYJMy'><pre id='eYJMy'><center id='eYJMy'></center></pre></bdo></b><th id='eYJMy'></th></span></q></dt></tr></i><div id='eYJMy'><tfoot id='eYJMy'></tfoot><dl id='eYJMy'><fieldset id='eYJMy'></fieldset></dl></div>
            • <bdo id='eYJMy'></bdo><ul id='eYJMy'></ul>

            • <tfoot id='eYJMy'></tfoot>

                <legend id='eYJMy'><style id='eYJMy'><dir id='eYJMy'><q id='eYJMy'></q></dir></style></legend>

                <small id='eYJMy'></small><noframes id='eYJMy'>

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

                问题描述

                限时送ChatGPT账号..
                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 函数的返回值)并在该进程上调用终止.但是,我不知道在使用该地图功能时该怎么做.

                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.

                我应该如何做到这一点?

                How should I achieve this?

                推荐答案

                您可以使用来自 Pool.apply_async 的回调.

                You can use callbacks from 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 多进程池.当其中一个工作进程确定不再需要完成工作时,如何退出脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何将队列引用传递给 pool.map_async() 管理的函数 下一篇:multiprocessing.pool.MaybeEncodingError: 'TypeError("

                相关文章

                最新文章

                  <legend id='ejd9d'><style id='ejd9d'><dir id='ejd9d'><q id='ejd9d'></q></dir></style></legend>
                  • <bdo id='ejd9d'></bdo><ul id='ejd9d'></ul>
                  1. <tfoot id='ejd9d'></tfoot>

                    <i id='ejd9d'><tr id='ejd9d'><dt id='ejd9d'><q id='ejd9d'><span id='ejd9d'><b id='ejd9d'><form id='ejd9d'><ins id='ejd9d'></ins><ul id='ejd9d'></ul><sub id='ejd9d'></sub></form><legend id='ejd9d'></legend><bdo id='ejd9d'><pre id='ejd9d'><center id='ejd9d'></center></pre></bdo></b><th id='ejd9d'></th></span></q></dt></tr></i><div id='ejd9d'><tfoot id='ejd9d'></tfoot><dl id='ejd9d'><fieldset id='ejd9d'></fieldset></dl></div>

                    <small id='ejd9d'></small><noframes id='ejd9d'>