我试图在进程之间共享一个字符串,这导致了一个没有错误输出的崩溃。我创建了一个展示行为的最小示例:
import multiprocessing as mp
from ctypes import c_wchar_p
import time
import random
alphabet = 'abcdefghijklmnopqrstuvwxyz'
class StringGenerator(mp.Process):
def __init__(self, *args, **kwargs):
mp.Process.__init__(self, *args, daemon=True, **kwargs)
self.sharedString = mp.Value(c_wchar_p, '')
def run(self):
print('generator init')
while True:
newString = ''.join(random.sample(alphabet, int(random.random()*10+5)))
self.sharedString.value = newString
print('generator created string:', newString)
time.sleep(0.5)
if __name__ == '__main__':
generator = StringGenerator()
generator.start()
print('starting')
while True:
time.sleep(0.69)
print('getting string from generator')
newString = generator.sharedString.value
print('new string:', newString)
print('done')
预期输出:
starting
generator init
generator created string: ftynxoerj
generator created string: wzpjbxsrfa
getting string from generator
new string: wzpjbxsrfa
generator created string: wejfiesl
getting string from generator
new string: wejfiesl
generator created string: eiofjwqa
generator created string: djlsjei
getting string from generator
new string: djlsjei
... etc etc
实际输出:
starting
generator init
generator created string: cywub
generator created string: vjnwrhgb
getting string from generator
就这样——我的交互式DOS提示回来了,没有错误消息,什么都没有。程序刚刚终止。我确实注意到,在任务管理器中,还有一个僵尸python进程在运行——我猜这就是StringGenerator进程仍在运行。
我尝试将共享值的get/set封装在锁获取/释放系统中,但它根本没有改变行为。
我在Windows 10上使用python 3.8.7。
有人能解释为什么会发生这种情况吗?我应该如何改变这种情况,以便在进程之间成功共享字符串?谢谢
感谢Michael Butscher的建议,我使用了一个用ctypes.c_wchar
和最大字符串长度初始化的multiprocessing.Array
对象来共享字符串。在复制到共享数组时,我还用空格填充了字符串,以确保最后一个字符串被完全替换。这是工作代码:
import multiprocessing as mp
from ctypes import c_wchar
import time
import random
alphabet = 'abcdefghijklmnopqrstuvwxyz'
class StringGenerator(mp.Process):
def __init__(self, *args, **kwargs):
mp.Process.__init__(self, *args, daemon=True, **kwargs)
self.sharedString = mp.Array(c_wchar, 16)
def run(self):
print('generator init')
while True:
newString = ''.join(random.sample(alphabet, int(random.random()*10+5)))
self.sharedString[:] = newString + ' '*(16 - len(newString))
print('generator created string:', newString)
time.sleep(0.5)
if __name__ == '__main__':
generator = StringGenerator()
generator.start()
print('starting')
while True:
time.sleep(0.69)
print('getting string from generator')
newString = generator.sharedString[:]
print('new string:', newString)
print('done')
输出:
starting
generator init
generator created string: ademxlp
generator created string: wivalymkhsneg
getting string from generator
new string: wivalymkhsneg
generator created string: frbmlwjypenvqa
getting string from generator
new string: frbmlwjypenvqa
generator created string: zdcbof
getting string from generator
new string: zdcbof
generator created string: leuzsmtrghvpfd
generator created string: ijcnquketolx
getting string from generator
new string: ijcnquketolx
generator created string: brtidjqnp
getting string from generator
new string: brtidjqnp
generator created string: zpjobdha
getting string from generator
new string: zpjobdha
generator created string: jpgbhf
...etc etc