久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

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

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

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

        使用多處理寫(xiě)入文件

        Writing to a file with multiprocessing(使用多處理寫(xiě)入文件)
        <i id='13cbO'><tr id='13cbO'><dt id='13cbO'><q id='13cbO'><span id='13cbO'><b id='13cbO'><form id='13cbO'><ins id='13cbO'></ins><ul id='13cbO'></ul><sub id='13cbO'></sub></form><legend id='13cbO'></legend><bdo id='13cbO'><pre id='13cbO'><center id='13cbO'></center></pre></bdo></b><th id='13cbO'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='13cbO'><tfoot id='13cbO'></tfoot><dl id='13cbO'><fieldset id='13cbO'></fieldset></dl></div>
          <bdo id='13cbO'></bdo><ul id='13cbO'></ul>

            • <small id='13cbO'></small><noframes id='13cbO'>

                <tfoot id='13cbO'></tfoot>

              1. <legend id='13cbO'><style id='13cbO'><dir id='13cbO'><q id='13cbO'></q></dir></style></legend>
                  <tbody id='13cbO'></tbody>

                  本文介紹了使用多處理寫(xiě)入文件的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

                  問(wèn)題描述

                  限時(shí)送ChatGPT賬號(hào)..

                  我在 python 中遇到以下問(wèn)題.

                  I'm having the following problem in python.

                  我需要并行進(jìn)行一些計(jì)算,其結(jié)果需要按順序?qū)懭胛募?所以我創(chuàng)建了一個(gè)接收 multiprocessing.Queue 和文件句柄的函數(shù),進(jìn)行計(jì)算并將結(jié)果打印到文件中:

                  I need to do some calculations in parallel whose results I need to be written sequentially in a file. So I created a function that receives a multiprocessing.Queue and a file handle, do the calculation and print the result in the file:

                  import multiprocessing
                  from multiprocessing import Process, Queue
                  from mySimulation import doCalculation   
                  
                  # doCalculation(pars) is a function I must run for many different sets of parameters and collect the results in a file
                  
                  def work(queue, fh):
                  while True:
                      try:
                          parameter = queue.get(block = False)
                          result = doCalculation(parameter) 
                          print >>fh, string
                      except:
                          break
                  
                  
                  if __name__ == "__main__":
                      nthreads = multiprocessing.cpu_count()
                      fh = open("foo", "w")
                      workQueue = Queue()
                      parList = # list of conditions for which I want to run doCalculation()
                      for x in parList:
                          workQueue.put(x)
                      processes = [Process(target = writefh, args = (workQueue, fh)) for i in range(nthreads)]
                      for p in processes:
                         p.start()
                      for p in processes:
                         p.join()
                      fh.close()
                  

                  但腳本運(yùn)行后文件最終為空.我試圖將 worker() 函數(shù)更改為:

                  But the file ends up empty after the script runs. I tried to change the worker() function to:

                  def work(queue, filename):
                  while True:
                      try:
                          fh = open(filename, "a")
                          parameter = queue.get(block = False)
                          result = doCalculation(parameter) 
                          print >>fh, string
                          fh.close()
                      except:
                          break
                  

                  并將文件名作為參數(shù)傳遞.然后它按我的意圖工作.當(dāng)我嘗試按順序執(zhí)行相同的操作時(shí),沒(méi)有多處理,它也可以正常工作.

                  and pass the filename as parameter. Then it works as I intended. When I try to do the same thing sequentially, without multiprocessing, it also works normally.

                  為什么它在第一個(gè)版本中不起作用?我看不出問(wèn)題.

                  Why it didn't worked in the first version? I can't see the problem.

                  另外:我可以保證兩個(gè)進(jìn)程不會(huì)同時(shí)嘗試寫(xiě)入文件嗎?

                  Also: can I guarantee that two processes won't try to write the file simultaneously?

                  謝謝.我現(xiàn)在明白了.這是工作版本:

                  Thanks. I got it now. This is the working version:

                  import multiprocessing
                  from multiprocessing import Process, Queue
                  from time import sleep
                  from random import uniform
                  
                  def doCalculation(par):
                      t = uniform(0,2)
                      sleep(t)
                      return par * par  # just to simulate some calculation
                  
                  def feed(queue, parlist):
                      for par in parlist:
                              queue.put(par)
                  
                  def calc(queueIn, queueOut):
                      while True:
                          try:
                              par = queueIn.get(block = False)
                              print "dealing with ", par, "" 
                              res = doCalculation(par)
                              queueOut.put((par,res))
                          except:
                              break
                  
                  def write(queue, fname):
                      fhandle = open(fname, "w")
                      while True:
                          try:
                              par, res = queue.get(block = False)
                              print >>fhandle, par, res
                          except:
                              break
                      fhandle.close()
                  
                  if __name__ == "__main__":
                      nthreads = multiprocessing.cpu_count()
                      fname = "foo"
                      workerQueue = Queue()
                      writerQueue = Queue()
                      parlist = [1,2,3,4,5,6,7,8,9,10]
                      feedProc = Process(target = feed , args = (workerQueue, parlist))
                      calcProc = [Process(target = calc , args = (workerQueue, writerQueue)) for i in range(nthreads)]
                      writProc = Process(target = write, args = (writerQueue, fname))
                  
                  
                      feedProc.start()
                      for p in calcProc:
                          p.start()
                      writProc.start()
                  
                      feedProc.join ()
                      for p in calcProc:
                          p.join()
                      writProc.join ()
                  

                  推薦答案

                  你真的應(yīng)該使用兩個(gè)隊(duì)列和三種不同的處理方式.

                  You really should use two queues and three separate kinds of processing.

                  1. 將東西放入隊(duì)列 #1.

                  1. Put stuff into Queue #1.

                  從 Queue #1 中取出東西并進(jìn)行計(jì)算,然后將東西放入 Queue #2.您可以擁有其中的許多,因?yàn)樗鼈儚囊粋€(gè)隊(duì)列中取出并安全地放入另一個(gè)隊(duì)列.

                  Get stuff out of Queue #1 and do calculations, putting stuff in Queue #2. You can have many of these, since they get from one queue and put into another queue safely.

                  從 Queue #2 中取出內(nèi)容并將其寫(xiě)入文件.您必須恰好擁有其中的 1 個(gè),僅此而已.它擁有"文件,保證原子訪問(wèn),并絕對(duì)保證文件被干凈和一致地寫(xiě)入.

                  Get stuff out of Queue #2 and write it to a file. You must have exactly 1 of these and no more. It "owns" the file, guarantees atomic access, and absolutely assures that the file is written cleanly and consistently.

                  這篇關(guān)于使用多處理寫(xiě)入文件的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                  相關(guān)文檔推薦

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

                      1. <legend id='0LeEj'><style id='0LeEj'><dir id='0LeEj'><q id='0LeEj'></q></dir></style></legend>
                          <tbody id='0LeEj'></tbody>

                        <tfoot id='0LeEj'></tfoot>
                        • <i id='0LeEj'><tr id='0LeEj'><dt id='0LeEj'><q id='0LeEj'><span id='0LeEj'><b id='0LeEj'><form id='0LeEj'><ins id='0LeEj'></ins><ul id='0LeEj'></ul><sub id='0LeEj'></sub></form><legend id='0LeEj'></legend><bdo id='0LeEj'><pre id='0LeEj'><center id='0LeEj'></center></pre></bdo></b><th id='0LeEj'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='0LeEj'><tfoot id='0LeEj'></tfoot><dl id='0LeEj'><fieldset id='0LeEj'></fieldset></dl></div>

                          <small id='0LeEj'></small><noframes id='0LeEj'>

                            主站蜘蛛池模板: 福利片在线| 一级毛片一级毛片 | 婷婷色av| 中文字幕+乱码+中文乱码91 | 亚洲精品在线视频观看 | 亚洲精品乱码久久久久久动漫 | 欧美日韩免费在线 | 亚洲国产成人精品女人 | 伊人免费视频 | 五月婷婷综合网 | 国产综合在线视频 | 日本成人一区二区三区 | 日韩中文字幕精品 | 国产天堂在线 | 欧美一级淫片bbb一84 | 欧美 日韩 国产 在线 | 97人人干 | 三级在线看| 谁有毛片网站 | 国产亚洲精品成人av久久ww | 日韩中文字幕视频 | 两性午夜视频 | av日韩精品| 伊人色播 | 亚洲免费视频一区 | 亚洲天堂偷拍 | 特级西西444www大精品视频 | 欧美大片黄 | 欧美激情第二页 | 色多多视频在线观看 | 夜夜躁狠狠躁日日躁av | 亚洲乱码在线观看 | 精品国产欧美一区二区三区成人 | 国产成人91| 国产精品一区二区三区不卡 | 日韩高清在线播放 | 日本婷婷 | 亚洲性猛交 | 久久只有精品 | 国产成人av在线播放 | 911亚洲精品|