• <tfoot id='3xQQA'></tfoot>

      <small id='3xQQA'></small><noframes id='3xQQA'>

      <i id='3xQQA'><tr id='3xQQA'><dt id='3xQQA'><q id='3xQQA'><span id='3xQQA'><b id='3xQQA'><form id='3xQQA'><ins id='3xQQA'></ins><ul id='3xQQA'></ul><sub id='3xQQA'></sub></form><legend id='3xQQA'></legend><bdo id='3xQQA'><pre id='3xQQA'><center id='3xQQA'></center></pre></bdo></b><th id='3xQQA'></th></span></q></dt></tr></i><div id='3xQQA'><tfoot id='3xQQA'></tfoot><dl id='3xQQA'><fieldset id='3xQQA'></fieldset></dl></div>
        <bdo id='3xQQA'></bdo><ul id='3xQQA'></ul>
        <legend id='3xQQA'><style id='3xQQA'><dir id='3xQQA'><q id='3xQQA'></q></dir></style></legend>
      1. Python 多处理 apply_async 永远不会在 Windows 7 上返回

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

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

                <tbody id='mY6n6'></tbody>

                  本文介绍了Python 多处理 apply_async 永远不会在 Windows 7 上返回结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在尝试遵循一个非常简单的多处理示例:

                  I am trying to follow a very simple multiprocessing example:

                  import multiprocessing as mp
                  
                  def cube(x):
                      return x**3
                  
                  pool    = mp.Pool(processes=2)
                  results = [pool.apply_async(cube, args=x) for x in range(1,7)]
                  

                  但是,在我的 windows 机器上,我无法获得结果(在 ubuntu 12.04LTS 上它运行完美).

                  However, on my windows machine, I am not able to get the result (on ubuntu 12.04LTS it runs perfectly).

                  如果我检查 results,我会看到以下内容:

                  If I inspect results, I see the following:

                  [<multiprocessing.pool.ApplyResult object at 0x01FF0910>,
                   <multiprocessing.pool.ApplyResult object at 0x01FF0950>,
                   <multiprocessing.pool.ApplyResult object at 0x01FF0990>,
                   <multiprocessing.pool.ApplyResult object at 0x01FF09D0>,
                   <multiprocessing.pool.ApplyResult object at 0x01FF0A10>,
                   <multiprocessing.pool.ApplyResult object at 0x01FF0A50>]
                  

                  如果我运行 results[0].ready() 我总是得到 False.

                  If I run results[0].ready() I always get False.

                  如果我运行 results[0].get(),python 解释器会冻结,等待得到永远不会出现的结果.

                  If I run results[0].get() the python interpreter freezes, waiting to get the result that never comes.

                  这个例子很简单,所以我认为这是一个与操作系统相关的低级错误(我在 Windows 7 上).但也许其他人有更好的主意?

                  The example is as simple as it gets, so I am thinking this is a low level bug relating to the OS (I am on Windows 7). But perhaps someone else has a better idea?

                  推荐答案

                  这里有几个错误.首先,您必须在 if __name__ == "__main__": 中声明 Pool 保护 在 Windows 上运行时.其次,您必须将 args 关键字参数传递一个序列,即使您只传递一个参数.所以把它们放在一起:

                  There are a couple of mistakes here. First, you must declare the Pool inside an if __name__ == "__main__": guard when running on Windows. Second, you have to pass the args keyword argument a sequence, even if you're only passing one argument. So putting that together:

                  import multiprocessing as mp
                  
                  def cube(x):
                      return x**3
                  
                  if __name__ == "__main__":
                      pool    = mp.Pool(processes=2)
                      results = [pool.apply_async(cube, args=(x,)) for x in range(1,7)]
                      print([result.get() for result in results])
                  

                  输出:

                  [1, 8, 27, 64, 125, 216]
                  

                  哦,正如 moarningsun 所提到的,multiprocessing 效果不佳 在交互式解释器中:

                  Oh, as moarningsun mentions, multiprocessing does not work well in the interactive interpreter:

                  注意

                  此包中的功能要求 __main__ 模块是孩子们可以导入.这在编程指南中有介绍但是值得在这里指出.这意味着一些例子,例如 multiprocessing.Pool 示例在交互式解释器.

                  Functionality within this package requires that the __main__ module be importable by the children. This is covered in Programming guidelines however it is worth pointing out here. This means that some examples, such as the multiprocessing.Pool examples will not work in the interactive interpreter.

                  因此,您需要将代码作为脚本实际执行才能正确测试.

                  So you'll need to actually execute the code as a script to test it properly.

                  这篇关于Python 多处理 apply_async 永远不会在 Windows 7 上返回结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:multiprocessing.pool.map 和带有两个参数的函数 下一篇:使用python多处理动态创建共享数组列表

                  相关文章

                  最新文章

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

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

                      <tfoot id='N8z8V'></tfoot>