How to share data between Python processes?

Dan picture Dan · Feb 2, 2016 · Viewed 16k times · Source

I'm using multiprocessing to create a sub-process to my Python app. I would like to share data between my parent process and the child process. it's important to mention that I need to share this asynchronously, means that the child process and the parent process will update the data during the code running.

What would be the best way to perform that?

Answer

AlokThakur picture AlokThakur · Feb 2, 2016

This is one simple example from python documentation -

from multiprocessing import Process, Queue

def f(q):
    q.put([42, None, 'hello'])

if __name__ == '__main__':
    q = Queue()
    p = Process(target=f, args=(q,))
    p.start()
    print q.get()    # prints "[42, None, 'hello']"
    p.join()

You can use pipe as well, Refer for more details - https://docs.python.org/2/library/multiprocessing.html