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
I think the original issue you were having (causing the TypeError
) was because the lock
argument to the multiprocessing.Value
constructor is a keyword-only argument. You need to call multiprocessing.Value("c", temp, lock=False)
in order for it to do what you were wanting to do.
However, I don't think you need to use a Value
object at all. You're passing the keycode as an argument to your other process, and the Value
isn't being used at all. I'd get rid of it completely:
def key(event):
instance = multiprocessing.Process(target=player, args=(event.char,))
instance.start()
There is no special syntax for multiprocessing.Value, it's just a class like any other. The signature of the Value
constructor is perfectly well described:
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”.
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.
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'
Update
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!'
For Python 3, use c_wchar_p
instead of c_char_p