我最难在进程间共享字符串。我查看了以下Q1,Q2,Q3,但在实际示例中仍然失败。如果我正确理解文档,则应该自动查看,因此设置/读取时不应存在竞争条件。这就是我所拥有的:
#!/usr/bin/env python3
import multiprocessing
import ctypes
import time
SLEEP = 0.1
CYCLES = 20
def child_process_fun(share):
for i in range(CYCLES):
time.sleep(SLEEP)
share.value = str(time.time())
if __name__ == '__main__':
share = multiprocessing.Value(ctypes.c_wchar_p, '')
process = multiprocessing.Process(target=child_process_fun, args=(share,))
process.start()
for i in range(CYCLES):
time.sleep(SLEEP)
print(share.value)
产生:
Traceback (most recent call last):
File "test2.py", line 23, in <module>
print(share.value)
File "<string>", line 5, in getvalue
ValueError: character U+e479b7b0 is not in range [U+0000; U+10ffff]
每个进程的编辑:'id(share.value)'都不同。但是,如果我尝试使用double作为共享变量,它们是相同的,它就像一个魅力。这可能是一个python bug吗?