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

  • <small id='haZAL'></small><noframes id='haZAL'>

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

        Python 中的 ProcessPoolExecutor 和 Lock

        时间:2023-08-06
            <tbody id='hnDu3'></tbody>
          • <small id='hnDu3'></small><noframes id='hnDu3'>

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

              <tfoot id='hnDu3'></tfoot>
                <bdo id='hnDu3'></bdo><ul id='hnDu3'></ul>

                • 本文介绍了Python 中的 ProcessPoolExecutor 和 Lock的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试将 concurrent.futures.ProcessPoolExecutor 与 Locks 一起使用,但出现运行时错误.(如果相关,我正在使用 Windows)

                  I am trying to use concurrent.futures.ProcessPoolExecutor with Locks, but I'm getting a run time error. (I'm working on Windows if that's relevant)

                  这是我的代码:

                  import multiprocessing
                  from concurrent.futures import ProcessPoolExecutor
                  
                  import time
                  
                  
                  def f(i, lock):
                      with lock:
                          print(i, 'hello')
                          time.sleep(1)
                          print(i, 'world')
                  
                  
                  def main():
                      lock = multiprocessing.Lock()
                      pool = ProcessPoolExecutor()
                      futures = [pool.submit(f, num, lock) for num in range(3)]
                      for future in futures:
                          future.result()
                  
                  
                  if __name__ == '__main__':
                      main()
                  

                  这是我得到的错误:

                      Traceback (most recent call last):
                    File "F:WinPython-64bit-3.4.3.2python-3.4.3.amd64Libmultiprocessingqueues.py", line 242, in _feed
                      obj = ForkingPickler.dumps(obj)
                    File "F:WinPython-64bit-3.4.3.2python-3.4.3.amd64Libmultiprocessing
                  eduction.py", line 50, in dumps
                      cls(buf, protocol).dump(obj)
                    File "F:WinPython-64bit-3.4.3.2python-3.4.3.amd64Libmultiprocessingsynchronize.py", line 102, in __getstate__
                      context.assert_spawning(self)
                    File "F:WinPython-64bit-3.4.3.2python-3.4.3.amd64Libmultiprocessingcontext.py", line 347, in assert_spawning
                      ' through inheritance' % type(obj).__name__
                  RuntimeError: Lock objects should only be shared between processes through inheritance
                  

                  奇怪的是,如果我用 multiprocessing.Process 编写相同的代码,一切正常:

                  What's weird is that if I write the same code with multiprocessing.Process it all works fine:

                  import multiprocessing
                  
                  import time
                  
                  
                  def f(i, lock):
                      with lock:
                          print(i, 'hello')
                          time.sleep(1)
                          print(i, 'world')
                  
                  
                  def main():
                      lock = multiprocessing.Lock()
                      processes = [multiprocessing.Process(target=f, args=(i, lock)) for i in range(3)]
                      for process in processes:
                          process.start()
                      for process in processes:
                          process.join()
                  
                  
                  
                  if __name__ == '__main__':
                      main()
                  

                  这行得通,我得到:

                  1 hello
                  1 world
                  0 hello
                  0 world
                  2 hello
                  2 world
                  

                  推荐答案

                  你需要使用 Manager 并使用 Manager.Lock() 代替:

                  You need to use a Manager and use a Manager.Lock() instead:

                  import multiprocessing
                  from concurrent.futures import ProcessPoolExecutor
                  
                  import time
                  
                  def f(i, lock):
                      with lock:
                          print(i, 'hello')
                          time.sleep(1)
                          print(i, 'world')
                  
                  def main():
                      pool = ProcessPoolExecutor()
                      m = multiprocessing.Manager()
                      lock = m.Lock()
                      futures = [pool.submit(f, num, lock) for num in range(3)]
                      for future in futures:
                          future.result()
                  
                  
                  if __name__ == '__main__':
                      main()
                  

                  结果:

                  % python locks.py
                  0 hello
                  0 world
                  1 hello
                  1 world
                  2 hello
                  2 world
                  

                  这篇关于Python 中的 ProcessPoolExecutor 和 Lock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 django 视图中使用 python 多处理模块 下一篇:与 Python 中的多处理相比,concurrent.futures 有哪些

                  相关文章

                  最新文章

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

                      <small id='4mJ3K'></small><noframes id='4mJ3K'>