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

        <bdo id='uGUIn'></bdo><ul id='uGUIn'></ul>

        <tfoot id='uGUIn'></tfoot>

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

        <legend id='uGUIn'><style id='uGUIn'><dir id='uGUIn'><q id='uGUIn'></q></dir></style></legend>
      1. 全局变量和 Python 多处理

        时间:2023-05-27
        <legend id='8Bhpv'><style id='8Bhpv'><dir id='8Bhpv'><q id='8Bhpv'></q></dir></style></legend>
          <tbody id='8Bhpv'></tbody>
          1. <i id='8Bhpv'><tr id='8Bhpv'><dt id='8Bhpv'><q id='8Bhpv'><span id='8Bhpv'><b id='8Bhpv'><form id='8Bhpv'><ins id='8Bhpv'></ins><ul id='8Bhpv'></ul><sub id='8Bhpv'></sub></form><legend id='8Bhpv'></legend><bdo id='8Bhpv'><pre id='8Bhpv'><center id='8Bhpv'></center></pre></bdo></b><th id='8Bhpv'></th></span></q></dt></tr></i><div id='8Bhpv'><tfoot id='8Bhpv'></tfoot><dl id='8Bhpv'><fieldset id='8Bhpv'></fieldset></dl></div>

              1. <small id='8Bhpv'></small><noframes id='8Bhpv'>

              2. <tfoot id='8Bhpv'></tfoot>

                  <bdo id='8Bhpv'></bdo><ul id='8Bhpv'></ul>
                  本文介绍了全局变量和 Python 多处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  可能重复:
                  Python 多处理全局变量更新未返回给父级

                  我正在使用具有多个内核的计算机,为了提高性能,我真的应该使用多个内核.但是,我很困惑为什么这些代码没有达到我的预期:

                  I am using a computer with many cores and for performance benefits I should really use more than one. However, I'm confused why these bits of code don't do what I expect:

                  from multiprocessing import Process
                  
                  var = range(5)
                  def test_func(i):
                      global var
                      var[i] += 1
                  
                  if __name__ == '__main__':
                      jobs = []
                      for i in xrange(5):
                          p = Process(target=test_func,args=(i,))
                          jobs.append(p)
                          p.start()
                  
                  print var
                  

                  还有

                  from multiprocessing import Pool
                  
                  var = range(5)
                  def test_func(i):
                      global var
                      var[i] += 1
                  
                  if __name__ == '__main__':
                      p = Pool()
                      for i in xrange(5):
                          p.apply_async(test_func,[i])
                  
                  print var
                  

                  我希望结果是 [1, 2, 3, 4, 5] 但结果是 [0, 1, 2, 3, 4].

                  I expect the result to be [1, 2, 3, 4, 5] but the result is [0, 1, 2, 3, 4].

                  在将全局变量与进程一起使用时,我肯定遗漏了一些微妙之处.这甚至是要走的路还是我应该避免尝试以这种方式更改变量?

                  There must be some subtlety I'm missing in using global variables with processes. Is this even the way to go or should I avoid trying to change a variable in this manner?

                  推荐答案

                  如果您正在运行两个单独的进程,那么它们将不会共享相同的全局变量.如果要在进程之间传递数据,请查看使用 send 和 recv.看看 http://docs.python.org/library/multiprocessing.html#shared-state-between-processes 举一个与您正在做的类似的例子.

                  If you are running two separate processes, then they won't be sharing the same globals. If you want to pass the data between the processes, look at using send and recv. Take a look at http://docs.python.org/library/multiprocessing.html#sharing-state-between-processes for an example similar to what you're doing.

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

                  上一篇:在 Python 中使用 subprocess.Popen 检查进程的状态 下一篇:Python 多处理不断产生 pythonw.exe 进程而不做任何实

                  相关文章

                  最新文章

                • <tfoot id='qUPLC'></tfoot>

                    <bdo id='qUPLC'></bdo><ul id='qUPLC'></ul>
                • <small id='qUPLC'></small><noframes id='qUPLC'>

                  <legend id='qUPLC'><style id='qUPLC'><dir id='qUPLC'><q id='qUPLC'></q></dir></style></legend>

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