[英]Python shared string memory for multiprocessing
I'm trying to use a shared string variable between my Python processes, but it seems that I'm doing something wrong since I'm getting coredumps and invalid memory values. 我正在尝试在我的Python进程之间使用共享字符串变量,但似乎我做错了,因为我得到了coredumps和无效的内存值。
I use multiprocessing.Value
to create a ctypes.c_char_p
value and use the value
attribute to access it. 我使用multiprocessing.Value
创建一个ctypes.c_char_p
值并使用value
属性来访问它。 In my understanding of the Python docs the value attribute should be synchronized, as long it is an instance of Value
(contrary of an instance of RawValue
). 在我对Python文档的理解中,value属性应该是同步的,只要它是Value
的一个实例(与RawValue
的实例相反)。 Is that correct so far? 到目前为止这是正确的吗?
I've created a short example to demonstrate my use of Value
and to show the inconsistency while executing: 我创建了一个简短的例子来演示我对Value
使用并在执行时显示不一致:
from multiprocessing import Process, Value
from ctypes import c_char_p
def process(v):
while True:
val = v.value
print val
while val == v.value:
pass
v = Value(c_char_p, None)
p = Process(target=process, args=(v,))
p.start()
for i in range(1,999):
v.value = str(i)
p.terminate()
I think the problem may have been caused by using Value(c_char_p)
to hold a string value. 我认为问题可能是由于使用Value(c_char_p)
来保存字符串值。 If you want a string, you should probably just use multiprocessing.Array(c_char)
. 如果你想要一个字符串,你应该只使用multiprocessing.Array(c_char)
。
From the Python-reference: https://docs.python.org/2/library/multiprocessing.html 来自Python-reference: https : //docs.python.org/2/library/multiprocessing.html
your_string = Array('B', range(LENGHT))
You can take the identifier for the datatype from the table from the array module reference: https://docs.python.org/2/library/array.html 您可以从数组模块引用中的表中获取数据类型的标识符: https : //docs.python.org/2/library/array.html
I ran into a similar problem when I attempted to set up multiple processes to access a shared I/O resource. 当我尝试设置多个进程来访问共享I / O资源时,我遇到了类似的问题。 It seems that Windows doesn't share a global variable space between processes, and items passed as arguments are squashed and passed by value. 似乎Windows不在进程之间共享全局变量空间,并且作为参数传递的项目被压缩并按值传递。
This may not directly relate to your problem, but reading the discussion my help point you in the right direction: 这可能与您的问题没有直接关系,但阅读讨论我的帮助会指出您正确的方向:
This is very similar in function to your example, though subtly different. 这在功能上与你的例子非常相似,尽管略有不同。 Notice that the child process terminates on its own when it gets the None sentinel. 请注意,子进程在获得None sentinel时会自行终止。 The polling loop could consume less CPU if it were to use a timeout. 如果要使用超时,轮询循环可能会消耗更少的CPU。
from multiprocessing import Process, Pipe
def show_numbers(consumer):
while True:
if consumer.poll():
val = consumer.recv()
if val==None:
break
print(val)
(consumer,producer) = Pipe(False)
proc = Process(target=show_numbers, args=(consumer,))
proc.start()
for i in range(1,999):
producer.send(str(i))
producer.send(None)
proc.join()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.