Section XXI queue thread safe queue

. 1  Import Queue
 2  Import Threading
 . 3  Import Time
 . 4  
. 5  
. 6 Q Queue.Queue = (. 5) # class object specified generated can accommodate up element 
. 7 q.empty () # determines whether the number of elements in the queue is 0 
. 8  
. 9  DEF Q_Q ():
 10      '' ' basic functions using queue ' '' 
. 11      for X in Range (1,10 ):
 12 is          q.put (10 * X )
 13 is          # FIFO, is put to a first 10, the first is out of the 10 
14          IF q.full ():
 15              Print ( "Queue is full " )
 16              for X in Range (q.qsize ()):
 . 17                  Print (q.get ())
 18 is              BREAK 
. 19  
20 is  
21 is  class q_qq (Queue.Queue):
 22 is      ' '' 
23 is      GET () and put () method Block parameters
 24      GET queue is empty, block the thread
 25      PUT are the queue is full, block the thread
 26 is      ' '' 
27      # DEF the __init __ (Self, Q): 
28      #      self.q = Q 
29      DEF the get_value (Self):
 30          the while . 1 :
 31 is              Print (q.get ())
32     def put_value(self):
33         inde = 0
34         while 1 :
35             q.put(inde)
36             inde += 1
37             time.sleep(0.5)
38 
39 
40 def main():
41     q = q_qq(7)
42     t1 = threading.Thread(target = q.put_value)
43     t2 = threading.Thread(target = q.get_value)
44     t1.start()
45     t2.start()
46     
47 if __name__ == ' __Main__ ' :
 48      main ()

 

Guess you like

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