SHARE
TWEET

Untitled

a guest Jun 15th, 2019 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import multiprocessing as mp
  2. import time
  3.  
  4. class testClass(object):
  5.     def __init__(self,name):
  6.         self.name=name
  7.     def doSomething(self):
  8.         print("Object %s reporting!"%(self.name))
  9.  
  10. #function receives objects      
  11. def receiverFunction(receive_end):
  12.     while True:
  13.         #receive object from the pipe
  14.         try:
  15.             obj=receive_end.recv()
  16.         except EOFError as err:
  17.             print("nothing left in the queue, aborting receiver thread")
  18.             break
  19.         #use the received object
  20.         obj.doSomething()
  21.  
  22. #function generates objects  
  23. def producerFunction(send_end):
  24.     start=time.time()
  25.     i=0
  26.     #produce data every 50ms for 5s
  27.     while time.time()-start<1:
  28.         i+=1
  29.         send_end.send(testClass("Object%d"%(i)))
  30.         time.sleep(50e-3)
  31.     print("Closing the send_end in producer process...")
  32.     send_end.close()
  33.  
  34. if __name__=="__main__":
  35.     (receive_end,send_end)=mp.Pipe()
  36.  
  37.     p_recv=mp.Process(target=receiverFunction,args=[receive_end])
  38.     p_send=mp.Process(target=producerFunction,args=[send_end])
  39.     p_recv.start()
  40.     p_send.start()
  41.  
  42.     p_send.join()
  43.     send_end.close()
  44.     print("Closing send_end in parent process")
  45.     p_recv.join()
  46.      
  47. Object Object1 reporting!
  48. Object Object2 reporting!
  49. Object Object3 reporting!
  50. Object Object4 reporting!
  51. Object Object5 reporting!
  52. Object Object6 reporting!
  53. Object Object7 reporting!
  54. Object Object8 reporting!
  55. Object Object9 reporting!
  56. Object Object10 reporting!
  57. Object Object11 reporting!
  58. Object Object12 reporting!
  59. Object Object13 reporting!
  60. Object Object14 reporting!
  61. Object Object15 reporting!
  62. Object Object16 reporting!
  63. Object Object17 reporting!
  64. Object Object18 reporting!
  65. Object Object19 reporting!
  66. Object Object20 reporting!
  67. Closing the send_end in producer process...
  68. Closing send_end in parent process
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top