Skip to content Skip to sidebar Skip to footer

In Python, How Do You Get Data Back From A Particular Process Using Multiprocessing?

import multiprocessing as mp import time def build(q): print 'I build things' time.sleep(10) #return 42 q.put(42) def run(q): num = q.get() print num

Solution 1:

Your question is a little murky..the problem you are describing sounds synchronous so 3 processes are a little overkill.

Assuming you are just trying to pass values to run you could use the queue object.

import multiprocessing as mp
import time

def build(q):
    print 'I build things'
    time.sleep(5)
    q.put(42)
    return 

def run(q):
    while True:
        num = q.get()
        if num == 42:
            print 'I run after build is done'
            return
        else:
            print 'not the right number...'

    def get_number():
        return 41

if __name__ == '__main__':
    queue = mp.Queue()

    run_p = mp.Process(name='run process', target=run, args=(queue,))
    build_p = mp.Process(name='build process', target=build, args=(queue,))

    run_p.start()
    build_p.start()

    print 'waiting on build'
    build_p.join()
    print 'waiting on run'
    run_p.join()
    queue.close()
    print 'waiting on queue'
    queue.join_thread()
    print 'done'

Post a Comment for "In Python, How Do You Get Data Back From A Particular Process Using Multiprocessing?"