Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import multiprocessing
- import os
- import sys
- inputQ = multiprocessing.JoinableQueue()
- outputQ = multiprocessing.JoinableQueue()
- def child(q,say=False):
- '''
- Tell the child how to reply
- '''
- pid = os.getpid()
- while True:
- print "PID:{} says {}".format(pid,say)
- (task,status) = inputQ.get()
- inputQ.task_done()
- if status == True:
- outputQ.put("Child:{} processed {}".format(pid,task))
- else:
- print "Exiting child:{}".format(pid)
- break
- if __name__ == '__main__':
- #inputQ.put((1,False))
- #child(1,'happy')
- #Start the processes
- for speak in ['hello','I like kittens','My name is joe','why is the sky blue','what?']:
- c = multiprocessing.Process(target=child, args=(inputQ,speak))
- c.start()
- #Feed and fetch data
- for i in range(500):
- inputQ.put((i,True)) #i is the data, True signals the child process to keep working
- received = outputQ.get()
- print "Parant got {}".format(received)
- outputQ.task_done()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement