<legend id='wVN80'><style id='wVN80'><dir id='wVN80'><q id='wVN80'></q></dir></style></legend>
      • <bdo id='wVN80'></bdo><ul id='wVN80'></ul>
    1. <i id='wVN80'><tr id='wVN80'><dt id='wVN80'><q id='wVN80'><span id='wVN80'><b id='wVN80'><form id='wVN80'><ins id='wVN80'></ins><ul id='wVN80'></ul><sub id='wVN80'></sub></form><legend id='wVN80'></legend><bdo id='wVN80'><pre id='wVN80'><center id='wVN80'></center></pre></bdo></b><th id='wVN80'></th></span></q></dt></tr></i><div id='wVN80'><tfoot id='wVN80'></tfoot><dl id='wVN80'><fieldset id='wVN80'></fieldset></dl></div>

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

        <tfoot id='wVN80'></tfoot>

        如何在导入的模块中使用 multiprocessing.Pool?

        时间:2023-05-27
          1. <small id='Y7gQR'></small><noframes id='Y7gQR'>

              <tbody id='Y7gQR'></tbody>
              <tfoot id='Y7gQR'></tfoot>

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

                  本文介绍了如何在导入的模块中使用 multiprocessing.Pool?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我无法在这里实施建议:同时对两个列表应用两个函数.

                  I have not been able to implement the suggestion here: Applying two functions to two lists simultaneously.

                  我猜是因为该模块是由另一个模块导入的,因此我的 Windows 产生了多个 python 进程?

                  I guess it is because the module is imported by another module and thus my Windows spawns multiple python processes?

                  我的问题是:我如何在没有 if if __name__ == "__main__":

                  My question is: how can I use the code below without the if if __name__ == "__main__":

                  args_m = [(mortality_men, my_agents, graveyard, families, firms, year, agent) for agent in males]
                  args_f = [(mortality_women, fertility, year, families, my_agents, graveyard, firms, agent) for agent in females]
                  
                  with mp.Pool(processes=(mp.cpu_count() - 1)) as p:
                      p.map_async(process_males, args_m)
                      p.map_async(process_females, args_f)
                  

                  process_malesprocess_females 都是函数.args_m, args_f 是迭代器

                  Both process_males and process_females are fuctions. args_m, args_f are iterators

                  另外,我不需要返回任何东西.代理是需要更新的类实例.

                  Also, I don't need to return anything. Agents are class instances that need updating.

                  推荐答案

                  if __name__ == '__main__': 的想法是避免无限进程产生.

                  The idea of if __name__ == '__main__': is to avoid infinite process spawning.

                  当腌制在你的主脚本中定义的函数时,python 必须弄清楚你的主脚本的哪一部分是函数代码.它基本上会重新运行你的脚本.如果您创建 Pool 的代码在同一个脚本中并且不受if main"保护,那么通过尝试导入该函数,您将尝试启动另一个 Pool这将尝试启动另一个 Pool....

                  When pickling a function defined in your main script, python has to figure out what part of your main script is the function code. It will basically re run your script. If your code creating the Pool is in the same script and not protected by the "if main", then by trying to import the function, you will try to launch another Pool that will try to launch another Pool....

                  因此,您应该将函数定义与实际的主脚本分开:

                  Thus you should separate the function definitions from the actual main script:

                  from multiprocessing import Pool
                  
                  # define test functions outside main
                  # so it can be imported withou launching
                  # new Pool
                  def test_func():
                      pass
                  
                  if __name__ == '__main__':
                      with Pool(4) as p:
                          r = p.apply_async(test_func)
                          ... do stuff
                          result = r.get()
                  

                  这篇关于如何在导入的模块中使用 multiprocessing.Pool?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:父进程退出时如何让子进程存活? 下一篇:在执行 I/O 密集型任务时,20 个进程中的 400 个线

                  相关文章

                  最新文章

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

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