我想使用multiprocessing.Value在多个进程中使用一个变量,但是Python文档中的语法不清楚。有谁能告诉我应该用什么作为类型(我的变量是一个字母),以及把变量名放在哪里?
编辑
我试着用经理在流程之间分享我的信件。但我现在唯一拥有的是Value('ctypes.c_char_p', '
(点击此处的键)')
打印在Python Shell中,仍然没有声音。
使用管理器时,控制台的速度似乎也比平时慢一些。从我按下键到屏幕上出现Value
之间几乎有一秒钟的延迟。
我的代码现在是这样的:
#Import
from tkinter import *
import wave
import winsound
import multiprocessing
#Functions
def key(event):
temp = event.char
manager = multiprocessing.Manager()
manager.Value(ctypes.c_char_p, temp)
hitkey = manager.Value(ctypes.c_char_p, temp)
instance = multiprocessing.Process(target=player, args=(hitkey,))
instance.start()
def player(hitkey):
print(hitkey + "1")
winsound.PlaySound(hitkey + '.wav', winsound.SND_FILENAME|winsound.SND_NOWAIT|winsound.SND_ASYNC)
if __name__ == "__main__":
#Initialisation
fenetre = Tk()
frame = Frame(fenetre, width=200, height=100)
#TK
frame.focus_set()
frame.bind("<Key>", key)
frame.pack()
fenetre.mainloop()
没有^{} 的特殊语法,它只是一个类,与其他类一样。构造方法的签名描述得很好:
您甚至有一些使用afterwards的例子。尤其是} 模块(例如
typecode_or_type
可以是文档中列出的^{'i'
用于有符号整数,'f'
用于浮点等)或ctypes
类型(如ctypes.c_int
等)的类型代码之一如果你想让一个
Value
包含一个字母,你可以:更新
代码的问题是typecode
'c'
表示字符而不是字符串。 如果要保存字符串,可以使用类型ctypes.c_char_p
:对于Python 3,使用
c_wchar_p
,而不是c_char_p
我认为您最初遇到的问题(导致
TypeError
)是因为lock
构造函数的multiprocessing.Value
参数是一个仅限关键字的参数。你需要打电话给multiprocessing.Value("c", temp, lock=False)
让它做你想做的事情。但是,我认为您根本不需要使用
Value
对象。您将密钥代码作为参数传递给另一个进程,而Value
根本没有被使用。我会彻底摆脱它:相关问题 更多 >
编程相关推荐