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
defbuild(q):
print'I build things'
time.sleep(5)
q.put(42)
returndefrun(q):
whileTrue:
num = q.get()
if num == 42:
print'I run after build is done'returnelse:
print'not the right number...'defget_number():
return41if __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?"