Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. import multiprocessing
  2. import os
  3. import sys
  4.  
  5. inputQ = multiprocessing.JoinableQueue()
  6. outputQ = multiprocessing.JoinableQueue()
  7.  
  8. def child(q,say=False):
  9. '''
  10. Tell the child how to reply
  11. '''
  12. pid = os.getpid()
  13.  
  14. while True:
  15. print "PID:{} says {}".format(pid,say)
  16. (task,status) = inputQ.get()
  17. inputQ.task_done()
  18. if status == True:
  19. outputQ.put("Child:{} processed {}".format(pid,task))
  20. else:
  21. print "Exiting child:{}".format(pid)
  22. break
  23.  
  24.  
  25.  
  26. if __name__ == '__main__':
  27. #inputQ.put((1,False))
  28. #child(1,'happy')
  29. #Start the processes
  30. for speak in ['hello','I like kittens','My name is joe','why is the sky blue','what?']:
  31. c = multiprocessing.Process(target=child, args=(inputQ,speak))
  32. c.start()
  33.  
  34. #Feed and fetch data
  35. for i in range(500):
  36. inputQ.put((i,True)) #i is the data, True signals the child process to keep working
  37. received = outputQ.get()
  38. print "Parant got {}".format(received)
  39. outputQ.task_done()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement