Section IV multi-process queue

. 1  Import multiprocessing
 2  
. 3  '' ' 
. 4  Q = multiprocessing.Queue (. 3)
 . 5  q.put (. 1)
 . 6  q.put_nowait (0)
 . 7  q.get ()
 . 8  q.get_nowait ()
 . 9  q.full ()
 10  q.empty ()
 . 11  the number of elements placed # exceeds a defined program in the blocked state as a get method. Both can be allowed by calling the given q.get_nowait () forced to exit the program
 12 is  ' '' 
13 is  
14  DEF put_data (Q):
 15      "" " write data " "" 
16      Data = [. 11, 22 is, 33 is, 44 is , 55, 66 ]
 . 17      for X in Data:
 18 is         q.put (X)
 . 19      Print ( ' ............. data write completion ............ ' )
 20 is  
21 is  DEF download_data (Q):
 22 is      "" " processing data ", "" 
23 is      data = List ()
 24      the while True:
 25          data.append (q.get ())
 26 is          iF q.empty ():   # when the data is not in the blocked state will take complete 
27              BREAK 
28      Print (data)
 29      Print ( ' data processing finished ' )
 30  
31 is  DEF main ():
 32     q = multiprocessing.Queue(10)
33     p2 = multiprocessing.Process(target=download_data, args=(q,))
34     p1 = multiprocessing.Process(target=put_data, args=(q,))
35     p1.start()
36     p2.start()
37 
38 
39 if __name__ == '__main__':
40     main()

 

Guess you like

Origin www.cnblogs.com/kogmaw/p/12575457.html