隊列queue

queue隊列 

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

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.python

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.app

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.less

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).ide

Queue. put_nowait (item)

Equivalent to put(item, False).fetch

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).ui

Queue. get_nowait ()

Equivalent to get(False).this

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

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被消費完畢

 

一、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'>

 

二、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])
相關文章
相關標籤/搜索