python – multiprocessing.value清晰的语法?
我想使用multiprocessing.Value在多个进程中使用变量,但
Python的文档中的语法并不清楚.任何人都可以告诉我应该使用什么作为类型(我的变量是一个字母),以及在哪里放置我的变量的名称?
编辑 我尝试使用管理器在进程之间共享我的信件.但我现在唯一拥有的是在Python Shell中打印的Value(‘ctypes.c_char_p’,'(你在这里点击的键)’),但仍然没有声音. 我的代码现在看起来像这样: #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() 解决方法multiprocessing.Value 没有特殊的语法,它只是一个类似于任何其他的类. Value构造函数的签名描述得非常好:
你甚至有一些使用它的例子afterwards.具体来说,typecode_or_type可以是 如果您想要包含单个字母的值,您可以执行以下操作: >>> import multiprocessing as mp >>> letter = mp.Value('c','A') >>> letter <Synchronized wrapper for c_char('A')> >>> letter.value 'A' 更新 您的代码的问题是类型代码’c’表示字符不是字符串. >>> import multiprocessing as mp >>> import ctypes >>> v = mp.Value('c',"Hello,World!") Traceback (most recent call last): File "<stdin>",line 1,in <module> File "/usr/lib/python2.7/multiprocessing/__init__.py",line 253,in Value return Value(typecode_or_type,*args,**kwds) File "/usr/lib/python2.7/multiprocessing/sharedctypes.py",line 99,in Value obj = RawValue(typecode_or_type,*args) File "/usr/lib/python2.7/multiprocessing/sharedctypes.py",line 73,in RawValue obj.__init__(*args) TypeError: one character string expected >>> v = mp.Value(ctypes.c_char_p,World!") >>> v <Synchronized wrapper for c_char_p(166841564)> >>> v.value 'Hello,World!' (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |