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

查看:49
本文介绍了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 ?

编辑

我试图使用Manager在进程之间共享我的信.但是我现在仅有的是Value('ctypes.c_char_p', '(您在此处击中的键)')打印在Python Shell中,但仍然没有声音. 使用管理器时,控制台似乎也比平时慢一些.从我敲键到屏幕上出现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 ,它就像其他任何类一样. 构造函数的签名已被完整描述:

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(默认值),则将创建一个新的锁对象以同步对该值的访问.如果锁是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'表示字符不是字符串. 如果要保留字符串,可以使用类型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天全站免登陆