Syntax syntax multiprocessing.value?

I want to use multiprocessing.Value to use a variable in several processes, but the syntax is not clear in the Python documentation. Can someone tell me what I should use as type (my variable is a letter) and where to indicate my variable name?

EDIT

I tried to use Manager to share my email between processes. But the only thing I have is Value('ctypes.c_char_p', ' (The key you clicked here) ') , printed in the Python shell and still without sound. When using the dispatcher, the console also looks a little slower than usual. There is almost one second delay between when I press a key and when Value appears on the screen.

Now my code 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() 
+6
python multiprocessing
source share
2 answers

There is no special syntax for multiprocessing.Value , it's just a class like any other. The signature of the Value constructor is perfectly described:

multiprocessing.Value(typecode_or_type, *args[, lock])

Returns 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 single character type of the type of the type being used using the array module. *args is passed to the constructor for type.

If lock is True (default), then a new lock object is created to synchronize access to the value. If the lock is lock or RLock , then this will be used to synchronize access to cost. If lock is False , then access to the returned object will not be automatically protected by the lock, so this is not necessary to be a "safe process".

You even have a few use cases. In particular, typecode_or_type can be one of the typecodes listed in the documentation for the array module (for example, 'i' for signed integer, 'f' for float, etc.) or the type ctypes , for example ctypes.c_int , etc. .

If you want to have a Value containing one letter you can do:

 >>> import multiprocessing as mp >>> letter = mp.Value('c', 'A') >>> letter <Synchronized wrapper for c_char('A')> >>> letter.value 'A' 

Update

The problem with your code is that typecode 'c' means the string is not . If you want to save the string, you can use the ctypes.c_char_p type:

 >>> 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!' 
+7
source share

I think the original problem you encountered (by raising TypeError ) was that the lock argument to the multiprocessing.Value constructor is an argument only for the keyword. You need to call multiprocessing.Value("c", temp, lock=False) so that it does what you want to do.

However, I do not think you need to use a Value object at all. You pass the key code as an argument to your other process, and Value is not used at all. I would completely get rid of him:

 def key(event): instance = multiprocessing.Process(target=player, args=(event.char,)) instance.start() 
+1
source share

All Articles