Python3:包含进程之间字符串的共享数组

Python3:包含进程之间字符串的共享数组,第1张

概述我想在进程之间共享一个包含字符串的列表,但不幸的是我收到错误消息“ValueError:字符U 169ea10不在范围[U 0000; U 10ffff]”中. 这是Python 3代码: from multiprocessing import Process, Array, Lockfrom ctypes import c_wchar_pimport timedef run_child( 我想在进程之间共享一个包含字符串的列表,但不幸的是我收到错误消息“ValueError:字符U 169ea10不在范围[U 0000; U 10ffff]”中.

这是Python 3代码:

from multiprocessing import Process,Array,Lockfrom ctypes import c_wchar_pimport timedef run_child(a):    time.sleep(2)    print(a[0]) # print foo    print(a[1]) # print bar    print(a[2]) # print baz    print("SET foofoo barbar bazbaz")    a[0] = "foofoo"    a[1] = "barbar"    a[2] = "bazbaz"lock = Lock()a = Array(c_wchar_p,range(3),lock=lock)p = Process(target=run_child,args=(a,))p.start()print("SET foo bar baz")a[0] = "foo"a[1] = "bar"a[2] = "baz"time.sleep(4)print(a[0]) # print foofooprint(a[1]) # print barbarprint(a[2]) # print bazbaz

有人知道我做错了什么吗?

问候
强尼

解决方法 您的ctype与Array的内容不匹配.您的初始化数据应该是一个字符串列表,以匹配您指定的ctype.您正在使用range(3)初始化它,它将计算为整数,而不是字符串.

应该更像

a = Array(c_wchar_p,('','',''),lock=lock)

从docs

c_wchar_p

Represents the C wchar_t * datatype,which must be a pointer to a zero-terminated wIDe character string. The constructor accepts an integer address,or a string.

总结

以上是内存溢出为你收集整理的Python3:包含进程之间字符串的共享数组全部内容,希望文章能够帮你解决Python3:包含进程之间字符串的共享数组所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/1197546.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-03
下一篇2022-06-03

发表评论

登录后才能评论

评论列表(0条)

    保存