gpt4 book ai didi

python - multiprocessing.value 清晰的语法?

转载 作者:太空狗 更新时间:2023-10-29 20:17:10 24 4
gpt4 key购买 nike

我想使用 multiprocessing.Value 在多个进程中使用一个变量,但是 Python 文档中的语法不清楚。谁能告诉我应该使用什么类型(我的变量是一个字母),以及在哪里放置我的变量名?

编辑

我尝试使用 Manager 在进程之间共享我的信件。但我现在唯一拥有的是 Value('ctypes.c_char_p', '(The key you hit here)') 在 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()

最佳答案

multiprocessing.Value 没有特殊语法,它只是一个像其他任何类一样的类。 Value 构造函数的签名 描述得非常好:

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 可以是 array 文档中列出的类型代码之一。模块(例如 'i' 表示有符号整数,'f' 表示 float 等)或 ctypes 类型,如 ctypes。 c_int

如果你想要一个包含单个字母的Value,你可以这样做:

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

更新

您的代码存在问题,类型代码'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

关于python - multiprocessing.value 清晰的语法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16484764/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com