pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

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

      <tfoot id='uhABa'></tfoot>

      1. <small id='uhABa'></small><noframes id='uhABa'>

        多處理 - 管道與隊列

        Multiprocessing - Pipe vs Queue(多處理 - 管道與隊列)
          <bdo id='QCRcc'></bdo><ul id='QCRcc'></ul>
          <tfoot id='QCRcc'></tfoot>

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

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

                1. <legend id='QCRcc'><style id='QCRcc'><dir id='QCRcc'><q id='QCRcc'></q></dir></style></legend>
                2. 本文介紹了多處理 - 管道與隊列的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  限時送ChatGPT賬號..

                  Python 的多處理包中的隊列和管道的根本區別是什么?p>

                  在什么情況下應該選擇一個而不是另一個?什么時候使用 Pipe() 比較有利?什么時候使用 Queue() 比較有利?

                  解決方案

                  • 一個 Pipe() 只能有兩個端點.

                  • 一個 Queue() 可以有多個生產者和消費者.

                  何時使用它們

                  如果您需要兩個以上的點進行通信,請使用 Queue().

                  如果您需要絕對性能,Pipe() 更快,因為 Queue() 是建立在 Pipe() 之上的.

                  績效基準測試

                  假設您想要生成兩個進程并盡快在它們之間發送消息.這些是使用 Pipe()Queue() 的類似測試之間的拉力賽的計時結果......這是在運行 Ubuntu 11.10 和 Python 2.7 的 ThinkpadT61 上.2.

                  僅供參考,我輸入了 JoinableQueue() 作為獎勵;JoinableQueue() 在調用 queue.task_done() 時計算任務(它甚至不知道具體的任務,它只計算隊列中未完成的任務),以便 queue.join() 知道工作已完成.

                  此答案底部的每個代碼...

                  mpenning@mpenning-T61:~$ python multi_pipe.py向 Pipe() 發送 10000 個數字需要 0.0369849205017 秒向 Pipe() 發送 100000 個數字需要 0.328398942947 秒向 Pipe() 發送 1000000 個數字需要 3.17266988754 秒mpenning@mpenning-T61:~$ python multi_queue.py向 Queue() 發送 10000 個號碼需要 0.105256080627 秒向 Queue() 發送 100000 個號碼需要 0.980564117432 秒向 Queue() 發送 1000000 個數字需要 10.1611330509 秒mpnening@mpenning-T61:~$ python multi_joinablequeue.py向 JoinableQueue() 發送 10000 個號碼需要 0.172781944275 秒向 JoinableQueue() 發送 100000 個號碼需要 1.5714070797 秒向 JoinableQueue() 發送 1000000 個號碼需要 15.8527247906 秒mpenning@mpenning-T61:~$

                  總而言之,Pipe()Queue() 快大約三倍.甚至不要考慮 JoinableQueue() 除非你真的必須有好處.

                  獎勵材料 2

                  多處理在信息流中引入了細微的變化,這使得調試變得困難,除非您知道一些捷徑.例如,您可能有一個腳本在許多條件下通過字典索引時工作正常,但很少會因某些輸入而失敗.

                  通常當整個python進程崩潰時我們會得到失敗的線索;但是,如果多處理功能崩潰,您不會將未經請求的崩潰回溯打印到控制臺.如果不知道是什么導致了進程崩潰,就很難追蹤未知的多進程崩潰.

                  我發現追蹤多處理崩潰信息的最簡單方法是將整個多處理函數包裝在 try/except 中并使用 traceback.print_exc():

                  導入回溯定義運行(自我,參數):嘗試:# 在這里插入要多處理的東西返回 args[0]['那個']除了:print "FATAL: reader({0}) exited while multiprocessing".format(args)追溯.print_exc()

                  現在,當您發現崩潰時,您會看到如下內容:

                  FATAL: reader([{'crash': 'this'}]) 在多處理時退出回溯(最近一次通話最后):__init__ 中的文件foo.py",第 19 行自我運行(參數)運行中的文件foo.py",第 46 行鍵錯誤:'那個'

                  源代碼:

                  <小時>

                  <代碼>"""multi_pipe.py"""從多處理導入過程,管道進口時間def reader_proc(管道):## 從管道中讀取;這將作為一個單獨的進程產生p_output, p_input = 管道p_input.close() # 我們只是在閱讀而真:msg = p_output.recv() # 從輸出管道讀取,什么也不做如果味精=='完成':休息def writer(計數,p_input):對于 xrange(0, count) 中的 ii:p_input.send(ii) # 將 'count' 數字寫入輸入管道p_input.send('完成')如果 __name__=='__main__':對于 [10**4, 10**5, 10**6] 中的計數:# 管道是單向的,有兩個端點:p_input ------>p_outputp_output, p_input = Pipe() # writer() 從 _this_ 進程寫入 p_inputreader_p = Process(target=reader_proc, args=((p_output, p_input),))reader_p.daemon = Truereader_p.start() # 啟動閱讀器進程p_output.close() # 我們不再需要這部分 Pipe()_start = time.time()writer(count, p_input) # 發送很多東西到 reader_proc()p_input.close()reader_p.join()print("向 Pipe() 發送 {0} 個數字需要 {1} 秒".format(count,(time.time() - _start)))

                  <小時>

                  <代碼>"""多隊列.py"""從多處理導入進程,隊列進口時間導入系統def reader_proc(隊列):## 從隊列中讀取;這將作為一個單獨的進程產生而真:msg = queue.get() # 從隊列中讀取,什么也不做如果(味精 == '完成'):休息def writer(計數,隊列):## 寫入隊列對于范圍內的 ii(0,計數):queue.put(ii) # 將 'count' 數寫入隊列queue.put('完成')如果 __name__=='__main__':pqueue = Queue() # writer() 從 _this_ 進程寫入 pqueue對于 [10**4, 10**5, 10**6] 中的計數:### reader_proc() 作為單獨的進程從 pqueue 讀取reader_p = Process(target=reader_proc, args=((pqueue),))reader_p.daemon = Truereader_p.start() # 啟動 reader_proc() 作為一個單獨的 python 進程_start = time.time()writer(count, pqueue) # 發送很多東西給 reader()reader_p.join() # 等待閱讀器完成print("發送 {0} 個數字到 Queue() 花費了 {1} 秒".format(count,(time.time() - _start)))

                  <小時>

                  <代碼>"""multi_joinablequeue.py"""從多處理導入流程,JoinableQueue進口時間def reader_proc(隊列):## 從隊列中讀取;這將作為一個單獨的進程產生而真:msg = queue.get() # 從隊列中讀取,什么也不做queue.task_done()def writer(計數,隊列):對于 xrange(0, count) 中的 ii:queue.put(ii) # 將 'count' 數寫入隊列如果 __name__=='__main__':對于 [10**4, 10**5, 10**6] 中的計數:jqueue = JoinableQueue() # writer() 從 _this_ 進程寫入 jqueue# reader_proc() 作為不同的進程從 jqueue 中讀取...reader_p = Process(target=reader_proc, args=((jqueue),))reader_p.daemon = Truereader_p.start() # 啟動閱讀器進程_start = time.time()writer(count, jqueue) # 向 reader_proc() 發送很多東西(在不同的進程中)jqueue.join() # 等待閱讀器完成print("向 JoinableQueue() 發送 {0} 個數字需要 {1} 秒".format(count,(time.time() - _start)))

                  What are the fundamental differences between queues and pipes in Python's multiprocessing package?

                  In what scenarios should one choose one over the other? When is it advantageous to use Pipe()? When is it advantageous to use Queue()?

                  解決方案

                  • A Pipe() can only have two endpoints.

                  • A Queue() can have multiple producers and consumers.

                  When to use them

                  If you need more than two points to communicate, use a Queue().

                  If you need absolute performance, a Pipe() is much faster because Queue() is built on top of Pipe().

                  Performance Benchmarking

                  Let's assume you want to spawn two processes and send messages between them as quickly as possible. These are the timing results of a drag race between similar tests using Pipe() and Queue()... This is on a ThinkpadT61 running Ubuntu 11.10, and Python 2.7.2.

                  FYI, I threw in results for JoinableQueue() as a bonus; JoinableQueue() accounts for tasks when queue.task_done() is called (it doesn't even know about the specific task, it just counts unfinished tasks in the queue), so that queue.join() knows the work is finished.

                  The code for each at bottom of this answer...

                  mpenning@mpenning-T61:~$ python multi_pipe.py 
                  Sending 10000 numbers to Pipe() took 0.0369849205017 seconds
                  Sending 100000 numbers to Pipe() took 0.328398942947 seconds
                  Sending 1000000 numbers to Pipe() took 3.17266988754 seconds
                  mpenning@mpenning-T61:~$ python multi_queue.py 
                  Sending 10000 numbers to Queue() took 0.105256080627 seconds
                  Sending 100000 numbers to Queue() took 0.980564117432 seconds
                  Sending 1000000 numbers to Queue() took 10.1611330509 seconds
                  mpnening@mpenning-T61:~$ python multi_joinablequeue.py 
                  Sending 10000 numbers to JoinableQueue() took 0.172781944275 seconds
                  Sending 100000 numbers to JoinableQueue() took 1.5714070797 seconds
                  Sending 1000000 numbers to JoinableQueue() took 15.8527247906 seconds
                  mpenning@mpenning-T61:~$
                  

                  In summary Pipe() is about three times faster than a Queue(). Don't even think about the JoinableQueue() unless you really must have the benefits.

                  BONUS MATERIAL 2

                  Multiprocessing introduces subtle changes in information flow that make debugging hard unless you know some shortcuts. For instance, you might have a script that works fine when indexing through a dictionary in under many conditions, but infrequently fails with certain inputs.

                  Normally we get clues to the failure when the entire python process crashes; however, you don't get unsolicited crash tracebacks printed to the console if the multiprocessing function crashes. Tracking down unknown multiprocessing crashes is hard without a clue to what crashed the process.

                  The simplest way I have found to track down multiprocessing crash informaiton is to wrap the entire multiprocessing function in a try / except and use traceback.print_exc():

                  import traceback
                  def run(self, args):
                      try:
                          # Insert stuff to be multiprocessed here
                          return args[0]['that']
                      except:
                          print "FATAL: reader({0}) exited while multiprocessing".format(args) 
                          traceback.print_exc()
                  

                  Now, when you find a crash you see something like:

                  FATAL: reader([{'crash': 'this'}]) exited while multiprocessing
                  Traceback (most recent call last):
                    File "foo.py", line 19, in __init__
                      self.run(args)
                    File "foo.py", line 46, in run
                      KeyError: 'that'
                  

                  Source Code:


                  """
                  multi_pipe.py
                  """
                  from multiprocessing import Process, Pipe
                  import time
                  
                  def reader_proc(pipe):
                      ## Read from the pipe; this will be spawned as a separate Process
                      p_output, p_input = pipe
                      p_input.close()    # We are only reading
                      while True:
                          msg = p_output.recv()    # Read from the output pipe and do nothing
                          if msg=='DONE':
                              break
                  
                  def writer(count, p_input):
                      for ii in xrange(0, count):
                          p_input.send(ii)             # Write 'count' numbers into the input pipe
                      p_input.send('DONE')
                  
                  if __name__=='__main__':
                      for count in [10**4, 10**5, 10**6]:
                          # Pipes are unidirectional with two endpoints:  p_input ------> p_output
                          p_output, p_input = Pipe()  # writer() writes to p_input from _this_ process
                          reader_p = Process(target=reader_proc, args=((p_output, p_input),))
                          reader_p.daemon = True
                          reader_p.start()     # Launch the reader process
                  
                          p_output.close()       # We no longer need this part of the Pipe()
                          _start = time.time()
                          writer(count, p_input) # Send a lot of stuff to reader_proc()
                          p_input.close()
                          reader_p.join()
                          print("Sending {0} numbers to Pipe() took {1} seconds".format(count,
                              (time.time() - _start)))
                  


                  """
                  multi_queue.py
                  """
                  
                  from multiprocessing import Process, Queue
                  import time
                  import sys
                  
                  def reader_proc(queue):
                      ## Read from the queue; this will be spawned as a separate Process
                      while True:
                          msg = queue.get()         # Read from the queue and do nothing
                          if (msg == 'DONE'):
                              break
                  
                  def writer(count, queue):
                      ## Write to the queue
                      for ii in range(0, count):
                          queue.put(ii)             # Write 'count' numbers into the queue
                      queue.put('DONE')
                  
                  if __name__=='__main__':
                      pqueue = Queue() # writer() writes to pqueue from _this_ process
                      for count in [10**4, 10**5, 10**6]:             
                          ### reader_proc() reads from pqueue as a separate process
                          reader_p = Process(target=reader_proc, args=((pqueue),))
                          reader_p.daemon = True
                          reader_p.start()        # Launch reader_proc() as a separate python process
                  
                          _start = time.time()
                          writer(count, pqueue)    # Send a lot of stuff to reader()
                          reader_p.join()         # Wait for the reader to finish
                          print("Sending {0} numbers to Queue() took {1} seconds".format(count, 
                              (time.time() - _start)))
                  


                  """
                  multi_joinablequeue.py
                  """
                  from multiprocessing import Process, JoinableQueue
                  import time
                  
                  def reader_proc(queue):
                      ## Read from the queue; this will be spawned as a separate Process
                      while True:
                          msg = queue.get()         # Read from the queue and do nothing
                          queue.task_done()
                  
                  def writer(count, queue):
                      for ii in xrange(0, count):
                          queue.put(ii)             # Write 'count' numbers into the queue
                  
                  if __name__=='__main__':
                      for count in [10**4, 10**5, 10**6]:
                          jqueue = JoinableQueue() # writer() writes to jqueue from _this_ process
                          # reader_proc() reads from jqueue as a different process...
                          reader_p = Process(target=reader_proc, args=((jqueue),))
                          reader_p.daemon = True
                          reader_p.start()     # Launch the reader process
                          _start = time.time()
                          writer(count, jqueue) # Send a lot of stuff to reader_proc() (in different process)
                          jqueue.join()         # Wait for the reader to finish
                          print("Sending {0} numbers to JoinableQueue() took {1} seconds".format(count, 
                              (time.time() - _start)))
                  

                  這篇關于多處理 - 管道與隊列的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  What exactly is Python multiprocessing Module#39;s .join() Method Doing?(Python 多處理模塊的 .join() 方法到底在做什么?)
                  Passing multiple parameters to pool.map() function in Python(在 Python 中將多個參數傳遞給 pool.map() 函數)
                  multiprocessing.pool.MaybeEncodingError: #39;TypeError(quot;cannot serialize #39;_io.BufferedReader#39; objectquot;,)#39;(multiprocessing.pool.MaybeEncodingError: TypeError(cannot serialize _io.BufferedReader object,)) - IT屋-程序員軟件開
                  Python Multiprocess Pool. How to exit the script when one of the worker process determines no more work needs to be done?(Python 多進程池.當其中一個工作進程確定不再需要完成工作時,如何退出腳本?) - IT屋-程序員
                  How do you pass a Queue reference to a function managed by pool.map_async()?(如何將隊列引用傳遞給 pool.map_async() 管理的函數?)
                  yet another confusion with multiprocessing error, #39;module#39; object has no attribute #39;f#39;(與多處理錯誤的另一個混淆,“模塊對象沒有屬性“f)
                  <tfoot id='RZ6uo'></tfoot>

                3. <legend id='RZ6uo'><style id='RZ6uo'><dir id='RZ6uo'><q id='RZ6uo'></q></dir></style></legend>

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

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

                          • 主站蜘蛛池模板: 欧美日韩国产一区二区三区不_久久久久国产精品无码不卡_亚洲欧洲美洲无码精品AV_精品一区美女视频_日韩黄色性爱一级视频_日本五十路人妻斩_国产99视频免费精品是看4_亚洲中文字幕无码一二三四区_国产小萍萍挤奶喷奶水_亚洲另类精品无码在线一区 | 冰雕-冰雪世界-大型冰雕展制作公司-赛北冰雕官网 | 广东泵阀展|阀门展-广东国际泵管阀展览会 | NM-02立式吸污机_ZHCS-02软轴刷_二合一吸刷软轴刷-厦门地坤科技有限公司 | 苹果售后维修点查询,苹果iPhone授权售后维修服务中心 – 修果网 拼装地板,悬浮地板厂家,悬浮式拼装运动地板-石家庄博超地板科技有限公司 | 一体化污水处理设备,一体化污水设备厂家-宜兴市福源水处理设备有限公司 | 三氯异氰尿酸-二氯-三氯-二氯异氰尿酸钠-优氯净-强氯精-消毒片-济南中北_优氯净厂家 | 培训一点通 - 合肥驾校 - 合肥新亚驾校 - 合肥八一驾校 | 上海办公室装修,办公楼装修设计,办公空间设计,企业展厅设计_写艺装饰公司 | 起好名字_取个好名字_好名网免费取好名在线打分 | 字典-新华字典-在线字典查字-字典趣 | 真空吸污车_高压清洗车厂家-程力专用汽车股份有限公司官网 | 桂林腻子粉_内墙外墙抗裂砂浆腻子粉推荐广西鑫达涂料厂家供应 | 无线讲解器-导游讲解器-自助讲解器-分区讲解系统 品牌生产厂家[鹰米讲解-合肥市徽马信息科技有限公司] | 双效节能浓缩器-热回流提取浓缩机组-温州市利宏机械 | 湖南教师资格网-湖南教师资格证考试网 | 一体化污水处理设备_生活污水处理设备_全自动加药装置厂家-明基环保 | 半自动预灌装机,卡式瓶灌装机,注射器灌装机,给药器灌装机,大输液灌装机,西林瓶灌装机-长沙一星制药机械有限公司 | 锌合金压铸-铝合金压铸厂-压铸模具-冷挤压-誉格精密压铸 | 防水接头-电缆防水接头-金属-电缆密封接头-不锈钢电缆接头 | 光纤测温-荧光光纤测温系统-福州华光天锐光电科技有限公司 | 窖井盖锯圆机_锯圆机金刚石锯片-无锡茂达金刚石有限公司 | 冷水机-冰水机-冷冻机-冷风机-本森智能装备(深圳)有限公司 | 江苏远邦专注皮带秤,高精度皮带秤,电子皮带秤研发生产 | 热风机_工业热风机生产厂家上海冠顶公司提供专业热风机图片价格实惠 | 污水提升器,污水提升泵,地下室排水,增压泵,雨水泵,智能供排水控制器-上海智流泵业有限公司 | 温州在线网| 高低温万能试验机-复合材料万能试验机-馥勒仪器 | 鑫铭东办公家具一站式定制采购-深圳办公家具厂家直销 | 电销卡 防封电销卡 不封号电销卡 电话销售卡 白名单电销卡 电销系统 外呼系统 | 带式过滤机厂家_价格_型号规格参数-江西核威环保科技有限公司 | 广州小程序开发_APP开发公司_分销商城系统定制_小跑科技 | TYPE-C厂家|TYPE-C接口|TYPE-C防水母座|TYPE-C贴片-深圳步步精 | 胶原检测试剂盒,弹性蛋白检测试剂盒,类克ELISA试剂盒,阿达木单抗ELISA试剂盒-北京群晓科苑生物技术有限公司 | 水上浮桥-游艇码头-浮动码头-游船码头-码瑞纳游艇码头工程 | 天津拓展_天津团建_天津趣味运动会_天津活动策划公司-天津华天拓展培训中心 | 纸塑分离机-纸塑分离清洗机设备-压力筛-碎浆机厂家金双联环保 | 外贸网站建设-外贸网站设计制作开发公司-外贸独立站建设【企术】 | 机器视觉检测系统-视觉检测系统-机器视觉系统-ccd检测系统-视觉控制器-视控一体机 -海克易邦 | 可程式恒温恒湿试验箱|恒温恒湿箱|恒温恒湿试验箱|恒温恒湿老化试验箱|高低温试验箱价格报价-广东德瑞检测设备有限公司 | 济南拼接屏_山东液晶拼接屏_济南LED显示屏—维康国际官网 |