multiprocessing.value 语法清晰吗? [英] multiprocessing.value clear syntax?

查看:17
本文介绍了multiprocessing.value 语法清晰吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

如果 lockTrue(默认值),则创建一个新的锁对象以同步对值的访问.如果 lock 是 LockRLock 对象然后将用于同步访问价值.如果 lockFalse 则访问返回的对象将不会被锁自动保护,所以不一定过程安全".

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 语法清晰吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆