<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 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>

        使用多处理写入文件

        时间:2023-05-27

          <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 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>
              <tbody id='pHvKB'></tbody>
              <bdo id='pHvKB'></bdo><ul id='pHvKB'></ul>

                  本文介绍了使用多处理写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我在 python 中遇到以下问题.

                  I'm having the following problem in python.

                  我需要并行进行一些计算,其结果需要按顺序写入文件中.所以我创建了一个接收 multiprocessing.Queue 和文件句柄的函数,进行计算并将结果打印到文件中:

                  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()
                  

                  但脚本运行后文件最终为空.我试图将 worker() 函数更改为:

                  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
                  

                  并将文件名作为参数传递.然后它按我的意图工作.当我尝试按顺序执行相同的操作时,没有多处理,它也可以正常工作.

                  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.

                  为什么它在第一个版本中不起作用?我看不出问题.

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

                  另外:我可以保证两个进程不会同时尝试写入文件吗?

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

                  谢谢.我现在明白了.这是工作版本:

                  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 ()
                  

                  推荐答案

                  你真的应该使用两个队列和三种不同的处理方式.

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

                  1. 将东西放入队列 #1.

                  1. Put stuff into Queue #1.

                  从 Queue #1 中取出东西并进行计算,然后将东西放入 Queue #2.您可以拥有其中的许多,因为它们从一个队列中取出并安全地放入另一个队列.

                  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 中取出内容并将其写入文件.您必须恰好拥有其中的 1 个,仅此而已.它拥有"文件,保证原子访问,并绝对保证文件被干净和一致地写入.

                  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.

                  这篇关于使用多处理写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何对“AttributeError: __exit__"进行故障排除在 下一篇:有没有办法将'stdin'作为参数传递给python中

                  相关文章

                  最新文章

                  • <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>

                      <tfoot id='0LeEj'></tfoot>
                    2. <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 id='0LeEj'><tfoot id='0LeEj'></tfoot><dl id='0LeEj'><fieldset id='0LeEj'></fieldset></dl></div>

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