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

        • <bdo id='klIFw'></bdo><ul id='klIFw'></ul>
        <legend id='klIFw'><style id='klIFw'><dir id='klIFw'><q id='klIFw'></q></dir></style></legend>
      1. <small id='klIFw'></small><noframes id='klIFw'>

        <tfoot id='klIFw'></tfoot>

      2. multiprocessing.Queue 的管道损坏错误

        时间:2023-08-06

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

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

              <tfoot id='Dv9kR'></tfoot>
              • <legend id='Dv9kR'><style id='Dv9kR'><dir id='Dv9kR'><q id='Dv9kR'></q></dir></style></legend>

                  本文介绍了multiprocessing.Queue 的管道损坏错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  在 python2.7 中,multiprocessing.Queue 在从函数内部初始化时会引发错误.我提供了一个重现问题的最小示例.

                  In python2.7, multiprocessing.Queue throws a broken error when initialized from inside a function. I am providing a minimal example that reproduces the problem.

                  #!/usr/bin/python
                  # -*- coding: utf-8 -*-
                  
                  import multiprocessing
                  
                  def main():
                      q = multiprocessing.Queue()
                      for i in range(10):
                          q.put(i)
                  
                  if __name__ == "__main__":
                      main()
                  

                  抛出下面的断管错误

                  Traceback (most recent call last):
                  File "/usr/lib64/python2.7/multiprocessing/queues.py", line 268, in _feed
                  send(obj)
                  IOError: [Errno 32] Broken pipe
                  
                  Process finished with exit code 0
                  

                  我无法解释原因.我们不能从函数内部填充 Queue 对象肯定会很奇怪.

                  I am unable to decipher why. It would certainly be strange that we cannot populate Queue objects from inside a function.

                  推荐答案

                  这里发生的是,当你调用 main() 时,它会创建 Queue,放入 10对象并结束函数,垃圾收集其内部的所有变量和对象,包括 Queue.但是您收到此错误是因为您仍在尝试发送 Queue 中的最后一个号码.

                  What happens here is that when you call main(), it creates the Queue, put 10 objects in it and ends the function, garbage collecting all of its inside variables and objects, including the Queue. BUT you get this error because you are still trying to send the last number in the Queue.

                  来自文档文档:

                  "当一个进程第一次将一个项目放入队列时,一个 feeder 线程是开始将对象从缓冲区传输到管道中."

                  "When a process first puts an item on the queue a feeder thread is started which transfers objects from a buffer into the pipe."

                  由于 put() 是在另一个 Thread 中进行的,它不会阻塞脚本的执行,并允许在完成之前结束 main() 函数队列操作.

                  As the put() is made in another Thread, it is not blocking the execution of the script, and allows to ends the main() function before completing the Queue operations.

                  试试这个:

                  #!/usr/bin/python
                  # -*- coding: utf-8 -*-
                  
                  import multiprocessing
                  import time
                  def main():
                      q = multiprocessing.Queue()
                      for i in range(10):
                          print i
                          q.put(i)
                      time.sleep(0.1) # Just enough to let the Queue finish
                  
                  if __name__ == "__main__":
                      main()
                  

                  应该有一种方法可以加入队列或阻止执行,直到将对象放入Queue,您应该查看文档.

                  There should be a way to join the Queue or block execution until the object is put in the Queue, you should take a look in the documentation.

                  这篇关于multiprocessing.Queue 的管道损坏错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:子 multiprocessing.Process 没有打印输出,除非程序崩 下一篇:Python 2.6 通过队列/管道/等发送连接对象

                  相关文章

                  最新文章

                1. <legend id='aYD8h'><style id='aYD8h'><dir id='aYD8h'><q id='aYD8h'></q></dir></style></legend>

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

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