問題描述
我想使用 multiprocessing.Value 在多個進程中使用變量,但 Python 文檔中的語法并不清楚.誰能告訴我應該使用什么類型(我的變量是一個字母),以及將變量名放在哪里?
I want to use?multiprocessing.Value?to use a variable in multiple processes, but the syntax is not clear on Python's documentation. Can anyone tell me what should I use as type (my variable is a letter), and where to put my variable's name ?
編輯
我嘗試使用管理器在進程之間共享我的信件.但我現在唯一擁有的是在 Python Shell 中打印的 Value('ctypes.c_char_p', '
(The key you hit here)')
仍然沒有聲音.使用管理器時,控制臺似乎也比平時慢了一點.在我按下鍵和 Value
出現在屏幕上之間,有將近一秒的延遲.
I tried using the?Manager?to share my letter between processes. But the only thing I have now is Value('ctypes.c_char_p', '
(The key you hit here)')
?printed in the Python Shell and still no sound.
The console also seems a bit slower than usual when using the manager. There's an almost one second delay between the time I hit the key and when the Value
appears on screen.
我的代碼現在看起來像這樣:
My code now looks like this :
#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])
返回一個從共享內存分配的 ctypes
對象.默認情況下,返回值實際上是對象的同步包裝器.
Return a ctypes
object allocated from shared memory. By default the return value is actually a synchronized wrapper for the object.
typecode_or_type
確定返回對象的類型:它是 ctypes
類型或所用類型的單字符類型代碼由 array
模塊.*args
被傳遞給構造函數輸入.
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.
如果 lock
為 True
(默認值),則創建一個新的鎖對象以同步對值的訪問.如果 lock 是 Lock
或RLock
對象然后將用于同步訪問價值.如果 lock
是 False
則訪問返回的對象將不會被鎖自動保護,所以不一定過程安全".
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".
您甚至還有一些使用示例之后一個>.特別是 typecode_or_type
可以是 array
模塊(例如 'i'
用于有符號整數,'f'
用于浮點等)或ctypes
類型,如 ctypes.c_int
等.
You even have some examples of its use afterwards. In particolar the typecode_or_type
can be one of the typecodes that are listed in the documentation for the array
module(e.g. 'i'
for signed integer, 'f'
for float etc.) or a ctypes
type, like ctypes.c_int
etc.
如果你想要一個包含單個字母的 Value
,你可以這樣做:
If you want to have a Value
containing a single letter you can do:
>>> import multiprocessing as mp
>>> letter = mp.Value('c', 'A')
>>> letter
<Synchronized wrapper for c_char('A')>
>>> letter.value
'A'
更新
您的代碼的問題是類型代碼 'c'
表示字符 not 字符串.如果你想保存一個字符串,你可以使用類型 ctypes.c_char_p
:
The problem with your code is that the typecode 'c'
means character not string.
If you want to hold a string you can use the type 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
這篇關于multiprocessing.value 語法清晰嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!