多处理。值清除语法?

2024-09-26 18:10:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用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()

Tags: keyimporteventvaluedefmanagerctypesmultiprocessing
2条回答

没有^{}的特殊语法,它只是一个类,与其他类一样。构造方法的签名描述得很好:

multiprocessing.Value(typecode_or_type, *args[, lock])

Return a ctypes object allocated from shared memory. By default the return value is actually a synchronized wrapper for the object.

typecode_or_type determines the type of the returned object: it is either a ctypes type or a one character typecode of the kind used by the array module. *args is passed on to the constructor for the type.

If lock is True (the default) then a new lock object is created to synchronize access to the value. If lock is a Lock or RLock object then that will be used to synchronize access to the value. If lock is False then access to the returned object will not be automatically protected by a lock, so it will not necessarily be “process-safe”.

您甚至有一些使用afterwards的例子。尤其是typecode_or_type可以是文档中列出的^{}模块(例如'i'用于有符号整数,'f'用于浮点等)或ctypes类型(如ctypes.c_int等)的类型代码之一

如果你想让一个Value包含一个字母,你可以:

>>> import multiprocessing as mp
>>> letter = mp.Value('c', 'A')
>>> letter
<Synchronized wrapper for c_char('A')>
>>> letter.value
'A'

更新

代码的问题是typecode'c'表示字符而不是字符串。 如果要保存字符串,可以使用类型ctypes.c_char_p

>>> 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, "Hello, World!")
>>> v
<Synchronized wrapper for c_char_p(166841564)>
>>> v.value
'Hello, World!'

对于Python 3,使用c_wchar_p,而不是c_char_p

我认为您最初遇到的问题(导致TypeError)是因为lock构造函数的multiprocessing.Value参数是一个仅限关键字的参数。你需要打电话给multiprocessing.Value("c", temp, lock=False)让它做你想做的事情。

但是,我认为您根本不需要使用Value对象。您将密钥代码作为参数传递给另一个进程,而Value根本没有被使用。我会彻底摆脱它:

def key(event): 
   instance = multiprocessing.Process(target=player, args=(event.char,)) 
   instance.start()

相关问题 更多 >

    热门问题