我正在 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() 尝试访问此时包含垃圾的无意义地址.
arrstores 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模板网!
Python 多处理模块的 .join() 方法到底在做什么?What exactly is Python multiprocessing Module#39;s .join() Method Doing?(Python 多处理模块的 .join() 方法到底在做什么?)
在 Python 中将多个参数传递给 pool.map() 函数Passing multiple parameters to pool.map() function in Python(在 Python 中将多个参数传递给 pool.map() 函数)
multiprocessing.pool.MaybeEncodingError: 'TypeError("multiprocessing.pool.MaybeEncodingError: #39;TypeError(quot;cannot serialize #39;_io.BufferedReader#39; objectquot;,)#39;(multiprocessing.pool.MaybeEnc
Python 多进程池.当其中一个工作进程确定不再需要Python Multiprocess Pool. How to exit the script when one of the worker process determines no more work needs to be done?(Python 多进程池.当其中一
如何将队列引用传递给 pool.map_async() 管理的函数How do you pass a Queue reference to a function managed by pool.map_async()?(如何将队列引用传递给 pool.map_async() 管理的函数?)
与多处理错误的另一个混淆,“模块"对象没yet another confusion with multiprocessing error, #39;module#39; object has no attribute #39;f#39;(与多处理错误的另一个混淆,“模块对象