• <small id='7G6Jv'></small><noframes id='7G6Jv'>

      <bdo id='7G6Jv'></bdo><ul id='7G6Jv'></ul>
  • <legend id='7G6Jv'><style id='7G6Jv'><dir id='7G6Jv'><q id='7G6Jv'></q></dir></style></legend>

      1. <tfoot id='7G6Jv'></tfoot>
        <i id='7G6Jv'><tr id='7G6Jv'><dt id='7G6Jv'><q id='7G6Jv'><span id='7G6Jv'><b id='7G6Jv'><form id='7G6Jv'><ins id='7G6Jv'></ins><ul id='7G6Jv'></ul><sub id='7G6Jv'></sub></form><legend id='7G6Jv'></legend><bdo id='7G6Jv'><pre id='7G6Jv'><center id='7G6Jv'></center></pre></bdo></b><th id='7G6Jv'></th></span></q></dt></tr></i><div id='7G6Jv'><tfoot id='7G6Jv'></tfoot><dl id='7G6Jv'><fieldset id='7G6Jv'></fieldset></dl></div>
      2. 在 Python 多进程中使用管理器更新队列

        Using a a manager for updating a Queue in a Python multiprocess(在 Python 多进程中使用管理器更新队列)
        <tfoot id='Jzt6E'></tfoot>
          <bdo id='Jzt6E'></bdo><ul id='Jzt6E'></ul>

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

                  <tbody id='Jzt6E'></tbody>
              1. <small id='Jzt6E'></small><noframes id='Jzt6E'>

                <legend id='Jzt6E'><style id='Jzt6E'><dir id='Jzt6E'><q id='Jzt6E'></q></dir></style></legend>
                • 本文介绍了在 Python 多进程中使用管理器更新队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在设计一个 Python 多处理代码以在一个队列中工作,该队列可能会在处理过程中更新.下面的代码有时会起作用,或者卡住,或者出现 Empty 错误.

                  I am designing a Python multiprocessing code to work in a queue that might be updated along the processing. The following code sometimes works, or get stuck, or rises an Empty error.

                  import multiprocessing as mp
                  
                  def worker(working_queue, output_queue):
                      while True:
                          if working_queue.empty() is True:
                              break    
                          else:
                              picked = working_queue.get_nowait()
                              if picked % 2 == 0: 
                                      output_queue.put(picked)
                              else:
                                  working_queue.put(picked+1)
                      return
                  
                  if __name__ == '__main__':
                      manager = mp.Manager()
                      static_input = xrange(100)    
                      working_q = manager.Queue()
                      output_q = mp.Queue()
                      for i in static_input:
                          working_q.put(i)
                      processes = [mp.Process(target=worker,args=(working_q, output_q)) for i in range(mp.cpu_count())]
                      for proc in processes:
                          proc.start()
                      for proc in processes:
                          proc.join()
                      results_bank = []
                      while True:
                         if output_q.empty() is True:
                             break
                         results_bank.append(output_q.get_nowait())
                      print len(results_bank) # length of this list should be equal to static_input, which is the range used to populate the input queue. In other words, this tells whether all the items placed for processing were actually processed.
                      results_bank.sort()
                      print results_bank
                  

                  我应该使用列表作为全局变量并锁定它,而不是 manager.Queue()?

                  Should I use a list as a global variable, and lock it, instead of a manager.Queue()?

                  推荐答案

                  我刚刚添加了一个 try:except Exception: 来处理 Empty 错误.现在的结果似乎是一致的.如果您发现我在此解决方案中忽略的问题,请告诉我.

                  I just added a try: and except Exception: to handle the Empty error. The results seem to be consistent now. Please let me know If you find problems I overlooked in this solution.

                  import multiprocessing as mp
                  
                  def worker(working_queue, output_queue):
                      while True:
                          try:
                              if working_queue.empty() is True:
                                  break  
                              else:
                                  picked = working_queue.get_nowait()
                                  if picked % 2 == 0: 
                                          output_queue.put(picked)
                                  else:
                                      working_queue.put(picked+1)
                          except Exception:
                              continue
                  
                      return
                  
                  if __name__ == '__main__':
                      #Manager seem to be unnecessary.
                      #manager = mp.Manager()
                      #working_q = manager.Queue()
                  
                      working_q = mp.Queue()
                      output_q = mp.Queue()
                      static_input = xrange(100)     
                      for i in static_input:
                          working_q.put(i)
                      processes = [mp.Process(target=worker,args=(working_q, output_q)) for i in range(mp.cpu_count())]
                      for proc in processes:
                          proc.start()
                      for proc in processes:
                          proc.join()
                      results_bank = []
                      while True:
                         if output_q.empty() is True:
                             break
                         results_bank.append(output_q.get_nowait())
                      print len(results_bank) # length of this list should be equal to static_input, which is the range used to populate the input queue. In other words, this tells whether all the items placed for processing were actually processed.
                      results_bank.sort()
                      print results_bank
                  

                  这篇关于在 Python 多进程中使用管理器更新队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Adding config modes to Plotly.Py offline - modebar(将配置模式添加到 Plotly.Py 离线 - 模式栏)
                  Plotly: How to style a plotly figure so that it doesn#39;t display gaps for missing dates?(Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙?)
                  python save plotly plot to local file and insert into html(python将绘图保存到本地文件并插入到html中)
                  Plotly: What color cycle does plotly express follow?(情节:情节表达遵循什么颜色循环?)
                  How to save plotly express plot into a html or static image file?(如何将情节表达图保存到 html 或静态图像文件中?)
                  Plotly: How to make a line plot from a pandas dataframe with a long or wide format?(Plotly:如何使用长格式或宽格式的 pandas 数据框制作线图?)
                    <bdo id='2Hetq'></bdo><ul id='2Hetq'></ul>
                        <tbody id='2Hetq'></tbody>
                      <i id='2Hetq'><tr id='2Hetq'><dt id='2Hetq'><q id='2Hetq'><span id='2Hetq'><b id='2Hetq'><form id='2Hetq'><ins id='2Hetq'></ins><ul id='2Hetq'></ul><sub id='2Hetq'></sub></form><legend id='2Hetq'></legend><bdo id='2Hetq'><pre id='2Hetq'><center id='2Hetq'></center></pre></bdo></b><th id='2Hetq'></th></span></q></dt></tr></i><div id='2Hetq'><tfoot id='2Hetq'></tfoot><dl id='2Hetq'><fieldset id='2Hetq'></fieldset></dl></div>
                    1. <tfoot id='2Hetq'></tfoot>

                      <small id='2Hetq'></small><noframes id='2Hetq'>

                            <legend id='2Hetq'><style id='2Hetq'><dir id='2Hetq'><q id='2Hetq'></q></dir></style></legend>