python operation queue

Operation queue, queue first to introduce this library

 

A: setting queue (queue brackets is how much data can be accommodated, if not set, may have been increased)

import queue

q = queue.Queue(10)

 

Two: Adding / Getting element

Use the queue put and get queue to add elements or acquisition

set_queue DEF (): 
    for I in Range (10): 
        q.put (I) # additive element 
    print (q.queue) # output queue entire 
    print (q.get ()) # Get element 
    print (q.queue) 



Output as follows:

and ([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
0
and ([1, 2, 3, 4, 5, 6, 7, 8, 9])

 We can see that once getting elements from the queue, the queue element no longer exists

 

Three: get the queue size

Use qsize method, the obtained size of the current queue.

set_queue DEF (): 
    for I in Range (10): 
        q.put (I) # additive element 
    print (q.qsize ()) # fetch size 


output:
10

 

IV: whether the queue is empty

set_queue DEF (): 
    Print (q.empty ()) # whether the queue is empty, empty returns True 
    for I in Range (10): 
        q.put (I) # additive element 
    print (q.qsize ()) # Get the size of the 
    print (q.empty ()) 


output is as follows:

True
10
False

  

*************** short step a thousand miles ***************

Guess you like

Origin www.cnblogs.com/liangxiyang/p/11867162.html