python queue module

1 queue module

  • Classification (common if maxsize <= 0 queue length is not limited.)

  • queue.Queue(maxsize =0)  First in first OUT(FIFO)

  • queue.LifoQueue (maxsize = 0) LIFO (Last In First Out: LIFO) queue

  • PriorityQueue (maxsize = 0) priority queue, comparing the queue size of each data, the value of the minimum data has priority queue. Typically in the form of data tuples inserted, typically in the form of (priority_number, data). If the data queue is not comparable, then the data will be packaged in a class, ignoring the data value, comparing only priority number.

  • Queue.SimpleQueue simple FIFO queue type, no size limit. Because it is a simple queue, the queue will be compared to the Queue lacks some advanced features

  • queue.Empty abnormal

  • queue.Full abnormal

2. queue method

  • Queue.qsize () Returns the number of data elements in the queue.

  • Queue.empty () if the queue is empty, returns True, otherwise False.

  • Queue.full () if the number of elements in the queue reaches an upper limit, returns True, otherwise return False.

  • Queue.put(item, block=True, timeout=None)

    • item, the data elements into the queue.
    • to block, the number of elements in the queue reaches an upper limit to go inside the discharge data: If block = False, directly lead queue.Full exception; if block = True, and timeout = None, then waits until the data queue can be released the data; if the block = True, and timeout = N, when N is a positive integer, then waits N seconds, if the queue is not yet placed in the position data is triggered queue.Full exception.
    • timeout, set the timeout
  • Queue.put_nowait (item) corresponding to Queue.put (item, block = False), when the number of elements in the queue reaches an upper limit to go inside the discharge data directly initiator queue.Full exception.

  • Queue.get(block=True, timeout=None)

    • to block, when there is no data in the queue continue to fetch data elements: If block = False, directly lead queue.Empty exception; if block = True, and timeout = None, then waits until the data can be removed after the data queues; If the block = True, and timeout = N, when N is a positive integer, then waits N seconds, if no data into the queue, then to initiate queue.Empty exception.
    • timeout, set the timeout
  • Queue.get_nowait () corresponds Queue.get (block = False) block, when there is no queue, the data elements continue to fetch data directly lead queue.Empty exception.

  • Queue.task_done () represents the data elements in the queue has been removed, i.e. for obtaining a get each data element, a subsequent call to tell task_done queue, process the data has been completed. If the called number is more than the number of elements placed in the queue, the initiator ValueError exception.

  • Queue. join() Block until all data elements in the queue have been taken out and executed, as long as the elements are added to the queue will increase. When the unfinished task count is equal to 0, join will not be blocked.

Guess you like

Origin www.cnblogs.com/tingxin/p/12229895.html