队列queue

queue队列 

queue is especially useful in threaded programming when information must be exchanged safely between multiple threads.

class queue.Queue(maxsize=0) # 先进先出
 1 import queue
 2 
 3 q = queue.Queue()
 4 
 5 q.put(123)
 6 q.put(456)
 7 print(q.qsize())
 8 print(q.get())
 9 
10 
11 # 输出结果:
12 2
13 123
queue.Queue
class queue.LifoQueue(maxsize=0) #last in fisrt out 
 1 import queue
 2 
 3 q2 = queue.LifoQueue()
 4 
 5 q2.put('abc')
 6 q2.put('def')
 7 
 8 print(q2.qsize())
 9 print(q2.get())
10 
11 
12 # 输出结果:
13 2
14 def
queue.LifoQueue
class queue.PriorityQueue(maxsize=0) #存储数据时可设置优先级的队列

Constructor for a priority queue. maxsize is an integer that sets the upperbound limit on the number of items that can be placed in the queue. Insertion will block once this size has been reached, until queue items are consumed. If maxsize is less than or equal to zero, the queue size is infinite.

The lowest valued entries are retrieved first (the lowest valued entry is the one returned by sorted(list(entries))[0]). A typical pattern for entries is a tuple in the form: (priority_number, data).

exception queue.Empty

Exception raised when non-blocking get() (or get_nowait()) is called on a Queue object which is empty.

import queue

q = queue.Queue()
# print(q.get())                    # 由于队列中没有数据,会一直在阻塞状态
# print(q.get(timeout=5))    # 过5秒钟之后,显示queue.Empty错误
print(q.get_nowait())        # 不等待,立即显示queue.Empty错误

  

exception queue.Full

Exception raised when non-blocking put() (or put_nowait()) is called on a Queue object which is full.

import queue

q = queue.Queue(maxsize=2)     # 最多放2个数据
q.put('abc')
q.put('def')
print(q.full())            # 结果True,刚好放了两个数据   
q.get_nowait()
print(q.full())            # 结果False,get_nowait取了一个数
Queue.qsize()
Queue.empty() #return True if empty  
Queue.full() # return True if full 
Queue.put(itemblock=Truetimeout=None)

Put item into the queue. If optional args block is true and timeout is None (the default), block if necessary until a free slot is available. If timeout is a positive number, it blocks at most timeout seconds and raises the Full exception if no free slot was available within that time. Otherwise (block is false), put an item on the queue if a free slot is immediately available, else raise the Full exception (timeout is ignored in that case).

Queue.put_nowait(item)

Equivalent to put(item, False).

Queue.get(block=Truetimeout=None)

Remove and return an item from the queue. If optional args block is true and timeout is None (the default), block if necessary until an item is available. If timeout is a positive number, it blocks at most timeout seconds and raises the Empty exception if no item was available within that time. Otherwise (block is false), return an item if one is immediately available, else raise the Empty exception (timeout is ignored in that case).

Queue.get_nowait()

Equivalent to get(False).

Two methods are offered to support tracking whether enqueued tasks have been fully processed by daemon consumer threads.

Queue.task_done()

Indicate that a formerly enqueued task is complete. Used by queue consumer threads. For each get() used to fetch a task, a subsequent call to task_done() tells the queue that the processing on the task is complete.

If a join() is currently blocking, it will resume when all items have been processed (meaning that a task_done() call was received for every item that had been put() into the queue).

Raises a ValueError if called more times than there were items placed in the queue.

Queue.join() block直到queue被消费完毕

1、queue中可存放任意数据类型 

import queue


class Foo:
    def __init__(self, a):
        self.a = a
        
q = queue.Queue()
data = q.put([1, 2, 3])
q.put(Foo(1))
print(q.get_nowait(), type(data))
data2 = q.get_nowait()
print(data2, type(data2))


# 输出结果:
[1, 2, 3] <class 'NoneType'>
<__main__.Foo object at 0x00000000011548D0> <class '__main__.Foo'>

2、PriorityQueue

import queue

q = queue.PriorityQueue(maxsize=30)      # 优先级队列,最多存放30个数据
q.put((5, 3), 2)                   # 元祖(5,3)中的5代表优先级,3是数据, 2是超时时间
q.put((3, ['a', 'b', 'c']))
q.put((50, 5))
print(q.get())
q.put((30, 2))
print(q.get())
print(q.get())
print(q.get())


# 输出结果:
(3, ['a', 'b', 'c'])
(5, 3)
(30, 2)
(50, 5)

  

双向队列 deque

  一个线程安全的双向队列

class deque(object):
    """
    deque([iterable[, maxlen]]) --> deque object
    
    Build an ordered collection with optimized access from its endpoints.
    """
    def append(self, *args, **kwargs): # real signature unknown
        """ Add an element to the right side of the deque. """
        pass

    def appendleft(self, *args, **kwargs): # real signature unknown
        """ Add an element to the left side of the deque. """
        pass

    def clear(self, *args, **kwargs): # real signature unknown
        """ Remove all elements from the deque. """
        pass

    def count(self, value): # real signature unknown; restored from __doc__
        """ D.count(value) -> integer -- return number of occurrences of value """
        return 0

    def extend(self, *args, **kwargs): # real signature unknown
        """ Extend the right side of the deque with elements from the iterable """
        pass

    def extendleft(self, *args, **kwargs): # real signature unknown
        """ Extend the left side of the deque with elements from the iterable """
        pass

    def pop(self, *args, **kwargs): # real signature unknown
        """ Remove and return the rightmost element. """
        pass

    def popleft(self, *args, **kwargs): # real signature unknown
        """ Remove and return the leftmost element. """
        pass

    def remove(self, value): # real signature unknown; restored from __doc__
        """ D.remove(value) -- remove first occurrence of value. """
        pass

    def reverse(self): # real signature unknown; restored from __doc__
        """ D.reverse() -- reverse *IN PLACE* """
        pass

    def rotate(self, *args, **kwargs): # real signature unknown
        """ Rotate the deque n steps to the right (default n=1).  If n is negative, rotates left. """
        pass

    def __copy__(self, *args, **kwargs): # real signature unknown
        """ Return a shallow copy of a deque. """
        pass

    def __delitem__(self, y): # real signature unknown; restored from __doc__
        """ x.__delitem__(y) <==> del x[y] """
        pass

    def __eq__(self, y): # real signature unknown; restored from __doc__
        """ x.__eq__(y) <==> x==y """
        pass

    def __getattribute__(self, name): # real signature unknown; restored from __doc__
        """ x.__getattribute__('name') <==> x.name """
        pass

    def __getitem__(self, y): # real signature unknown; restored from __doc__
        """ x.__getitem__(y) <==> x[y] """
        pass

    def __ge__(self, y): # real signature unknown; restored from __doc__
        """ x.__ge__(y) <==> x>=y """
        pass

    def __gt__(self, y): # real signature unknown; restored from __doc__
        """ x.__gt__(y) <==> x>y """
        pass

    def __iadd__(self, y): # real signature unknown; restored from __doc__
        """ x.__iadd__(y) <==> x+=y """
        pass

    def __init__(self, iterable=(), maxlen=None): # known case of _collections.deque.__init__
        """
        deque([iterable[, maxlen]]) --> deque object
        
        Build an ordered collection with optimized access from its endpoints.
        # (copied from class doc)
        """
        pass

    def __iter__(self): # real signature unknown; restored from __doc__
        """ x.__iter__() <==> iter(x) """
        pass

    def __len__(self): # real signature unknown; restored from __doc__
        """ x.__len__() <==> len(x) """
        pass

    def __le__(self, y): # real signature unknown; restored from __doc__
        """ x.__le__(y) <==> x<=y """
        pass

    def __lt__(self, y): # real signature unknown; restored from __doc__
        """ x.__lt__(y) <==> x<y """
        pass

    @staticmethod # known case of __new__
    def __new__(S, *more): # real signature unknown; restored from __doc__
        """ T.__new__(S, ...) -> a new object with type S, a subtype of T """
        pass

    def __ne__(self, y): # real signature unknown; restored from __doc__
        """ x.__ne__(y) <==> x!=y """
        pass

    def __reduce__(self, *args, **kwargs): # real signature unknown
        """ Return state information for pickling. """
        pass

    def __repr__(self): # real signature unknown; restored from __doc__
        """ x.__repr__() <==> repr(x) """
        pass

    def __reversed__(self): # real signature unknown; restored from __doc__
        """ D.__reversed__() -- return a reverse iterator over the deque """
        pass

    def __setitem__(self, i, y): # real signature unknown; restored from __doc__
        """ x.__setitem__(i, y) <==> x[i]=y """
        pass

    def __sizeof__(self): # real signature unknown; restored from __doc__
        """ D.__sizeof__() -- size of D in memory, in bytes """
        pass

    maxlen = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default
    """maximum size of a deque or None if unbounded"""


    __hash__ = None
deque源代码
import collections

d = collections.deque()
d.append(1)
d.appendleft(10)
d.appendleft(1)
print(d)
print(d.count(1))
d.extend(['yy', 'uu', 'ii'])
d.extendleft(['flash'])
print(d)
d.rotate()
print(d)
d.rotate(3)
print(d)


# 输出结果:
deque([1, 10, 1])
2
deque(['flash', 1, 10, 1, 'yy', 'uu', 'ii'])
deque(['ii', 'flash', 1, 10, 1, 'yy', 'uu'])
deque([1, 'yy', 'uu', 'ii', 'flash', 1, 10])

  

  

运维因自动化而有趣!
原文地址:https://www.cnblogs.com/Rambotien/p/5584039.html