加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – multiprocessing.value清晰的语法?

发布时间:2020-12-16 23:10:54 所属栏目:Python 来源:网络整理
导读:我想使用multiprocessing.Value在多个进程中使用变量,但 Python的文档中的语法并不清楚.任何人都可以告诉我应该使用什么作为类型(我的变量是一个字母),以及在哪里放置我的变量的名称? 编辑 我尝试使用管理器在进程之间共享我的信件.但我现在唯一拥有的是在P
我想使用multiprocessing.Value在多个进程中使用变量,但 Python的文档中的语法并不清楚.任何人都可以告诉我应该使用什么作为类型(我的变量是一个字母),以及在哪里放置我的变量的名称?

编辑

我尝试使用管理器在进程之间共享我的信件.但我现在唯一拥有的是在Python Shell中打印的Value(‘ctypes.c_char_p’,'(你在这里点击的键)’),但仍然没有声音.
使用管理器时,控制台似乎比平常慢一点.在我按下键和屏幕上显示值之间有几乎一秒的延迟.

我的代码现在看起来像这样:

#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等.

如果您想要包含单个字母的值,您可以执行以下操作:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读