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

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

        共享数组在 python 多处理中未正确共享

        时间:2023-05-26
          • <bdo id='YSa1Q'></bdo><ul id='YSa1Q'></ul>

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

                  <tfoot id='YSa1Q'></tfoot>

                • <legend id='YSa1Q'><style id='YSa1Q'><dir id='YSa1Q'><q id='YSa1Q'></q></dir></style></legend>
                • <small id='YSa1Q'></small><noframes id='YSa1Q'>

                  本文介绍了共享数组在 python 多处理中未正确共享的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在 Python 中尝试多处理,并尝试在两个进程之间共享一个字符串数组.这是我的python代码:

                  I am experimenting multiprocessing in Python and tried to share an Array of strings among two processes. Here is my python code :

                  from multiprocessing import Process, Array, Value
                  import ctypes
                  
                  def f1(a, v):
                      for i, l in enumerate(['a', 'b', 'c']):
                          a[i] = l*3
                  
                      v.value += 1
                  
                      print "f1 : ", a[:], v.value
                  
                  def f2(a,v):
                  
                      v.value += 1
                  
                      print "f2 : ", a[:], v.value
                  
                  if __name__ == '__main__':
                      val = Value(ctypes.c_int, 0)
                      arr = Array(ctypes.c_char_p, 3)
                  
                      print "Before :", arr[:], val.value
                  
                      p = Process(target=f1, args=(arr, val))
                      p2 = Process(target=f2, args=(arr, val))
                  
                      p.start()
                      p2.start()
                  
                      p.join()
                      p2.join()
                  
                      print "After : ", arr[:], val.value
                  

                  当我运行脚本时,我看到 arr 已正确填充并在 f1() 中可用,但在 f2() 中不可用.结果如下:

                  When I run the script I see that arr is correctly populated and available in f1() but not in f2(). Here is the result:

                      % python /tmp/tests.py
                      Before : [None, None, None] 0
                      f1 :  ['aaa', 'bbb', 'ccc'] 1
                      f2 :  ['x01', 'x11', 'x01'] 2
                      After :  ['x01', 'x01', 'x01'] 2
                  

                  我是否忽略了什么?

                  提前感谢您的反馈.:)

                  Thanks in advance for your feedback. :)

                  推荐答案

                  我的猜测是:

                  arr 存储 3 个指针.f1() 将它们分配给没有意思是在当前流程之外.f2() 尝试访问此时包含垃圾的无意义地址.

                  arr stores 3 pointers. f1() assigns them to memory addresses that have no meaning outside current process. f2() tries to access the meaningless addresses that contain junk at this point.

                  分配给在所有过程中都有意义的值似乎有帮助:

                  Assigning to values that have meaning in all processes seems to help:

                  from __future__ import print_function
                  import ctypes
                  import time
                  from multiprocessing import Process, Array, Value
                  
                  values = [(s*4).encode('ascii') for s in 'abc']
                  
                  def f1(a, v):
                      for i, s in enumerate(values):
                          a[i] = s
                  
                      v.value += 1
                  
                      print("f1 : ", a[:], v.value)
                  
                  def f2(a,v):
                      v.value += 1
                      print("f2 : ", a[:], v.value)
                  
                  def main():
                      val = Value(ctypes.c_int, 0)
                      arr = Array(ctypes.c_char_p, 3)
                  
                      print("Before :", arr[:], val.value)
                  
                      p = Process(target=f1, args=(arr, val))
                      p2 = Process(target=f2, args=(arr, val))
                  
                      p.start()
                      p2.start()
                  
                      p.join()
                      p2.join()
                  
                      print("After : ", arr[:], val.value)
                  
                  if __name__ == '__main__':
                      main()
                  

                  输出

                  Before : [None, None, None] 0
                  f1 :  ['aaaa', 'bbbb', 'cccc'] 1
                  f2 :  ['aaaa', 'bbbb', 'cccc'] 2
                  After :  ['aaaa', 'bbbb', 'cccc'] 2
                  

                  这篇关于共享数组在 python 多处理中未正确共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:多处理:在某些情况下,在赋值之前引用变量,但 下一篇:多处理不起作用(oserror:[Errno 22] Invalid argument)

                  相关文章

                  最新文章

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

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

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

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