- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想使用 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 actypes
type or a one character typecode of the kind used by thearray
module.*args
is passed on to the constructor for the type.If
lock
isTrue
(the default) then a new lock object is created to synchronize access to the value. If lock is aLock
orRLock
object then that will be used to synchronize access to the value. Iflock
isFalse
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/
你好,我是 Cone。 首先,我们思考一个问题。 为什么会有操作系统? 在我们教科书上会提到分时系统、批处理系统等等现代操作系统前的中间产物,也会讲到管理硬件的功能,但似乎没有讲到为什么有
假设我只想清除顶部的所有条目,清除 Map 的 Map 的最合适方法是什么 -关卡 map ? Map> nestedMap; 方法A:仅清除顶层 map 。 nestedMap.clear();
我时不时遇到一个问题,我不确定解决方案是什么。 我有一个 2 列布局(左边是 strit,然后是主要区域)。在主要区域,我有时会有一个次要的 2 栏布局(例如 - 对于新闻部分,那里有一个图标,然后是
我有以下代码: Created Created .clearfix:after{ clear: both; bdy: "."; display
有没有办法CLS单行输出?我不相信 CLS 有任何开关,所以也许更好的问题是: 有什么办法吗 保留所有以前的输出以供重复使用? 或 捕获当前显示的输出(就像通过标记和复制一样)? 我只是想通过实时反馈
我有一个流式布局。当布局足够宽时,一些 div(.one 和 .two)可以全部水平排列在一条线上。 当布局最终被挤压时,右侧 float 的 div (.two) 最终会出现在多行上。有没有一种方法
我面临着一个我真的不知道从哪里开始解决的问题,所以我希望这个问题不要太宽泛。 我正在制作并在屏幕上应用它,我将有一个包含一些信息的矩形(假设它是一个 )并且我需要用另一个矩形覆盖那个矩形,所以当用户
我的 UITableView 跨越 iPhone View 的大小,并有一个 tableHeaderView 保存附加内容。想要标题清晰,表格的其余部分白色,我将表格的背景颜色设置为清晰,并在 Vie
我一直在使用 ImageMagick,但它产生的结果非常模糊。 convert -density 300 ../images/favicons/procensus.svg -background tr
我有手动数据的工作项目,但我想在我的项目中添加 json 解析。我认为我需要帮助。 (必须是实时解析,如果可能的话,新的item添加时会自动释放) 我的 TableView 代码 - (void)sc
我正在制作一个响应式网站。我有 3 个 div (.block),我需要将它们水平放置在一起。 当屏幕足够宽时,这很容易实现。但是,当我使浏览器更窄时,第三个 div (3) 换行到下一行,但我想要的
如何在 Swift 3 中使这个 UITableView 和它的单元格清晰。 我已经完成了前面的线程,但我仍然得到一个白色背景。 正如您从我的代码中看到的,我已经尝试了提到的各种方法: overrid
当我使用 为了显示图标,它在谷歌浏览器中看起来非常清晰锐利。然而,当我在 Firefox 或 Internet Explorer 中打开 svg 时,图标看起来很模糊。 这些浏览器似乎将图标呈现为半像
我是一名优秀的程序员,十分优秀!