How to pass a string between multiple processes using Managers () in Python?

I need to read lines written by multiprocessor .Process instances from the main process. I already use Managers and queues to pass arguments to processes, so using managers seems obvious, but managers don't support strings :

The manager returned by manager () will maintain a list of types, dict, Namespace, Lock, RLock, Semaphore, BoundedSemaphore, Condition, Event, Queue, value and array.

How can I share the state represented by a string using Managers from the multiprocessing module?

+7
python shared-memory multiprocessing
source share
2 answers

multiprocessor managers can hold values , which, in turn, can contain instances of type c_char_p from the ctypes module:

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

See also: Message with an original solution that I found difficult to find.

Thus, the manager can be used to share strings under several processes in Python, for example:

 >>> from multiprocessing import Process, Manager, Value >>> from ctypes import c_char_p >>> >>> def greet(string): >>> string.value = string.value + ", World!" >>> >>> if __name__ == '__main__': >>> manager = Manager() >>> string = manager.Value(c_char_p, "Hello") >>> process = Process(target=greet, args=(string,)) >>> process.start() >>> process.join() >>> print string.value 'Hello, World!' 
+7
source share

Just enter the line in the dict :

 d = manager.dict() d['state'] = 'xyz' 

Since the strings themselves are immutable, splitting one directly will not be as useful.

+4
source share

All Articles