# put 向队列中添加数据 q.put(15) q.put(59) print(q.qsize())
# 超时时间为2秒 q.put('PolarSnow', timeout=2)
# 获取当前队列长度 print(q.qsize())
# 取出最前面的一个数据 print(q.get())
------------ 2 Traceback (most recent call last): File "/Users/lvrui/PycharmProjects/untitled/10/temp.py", line 157, in <module> q.put('PolarSnow', timeout=2) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/queue.py", line 141, in put raise Full queue.Full
# mutex must be held whenever the queue is mutating. All methods # that acquire mutex must release it before returning. mutex # is shared between the three conditions, so acquiring and # releasing the conditions also acquires and releases mutex. self.mutex = threading.Lock()
# Notify not_empty whenever an item is added to the queue; a # thread waiting to get is notified then. self.not_empty = threading.Condition(self.mutex)
# Notify not_full whenever an item is removed from the queue; # a thread waiting to put is notified then. self.not_full = threading.Condition(self.mutex)
# Notify all_tasks_done whenever the number of unfinished tasks # drops to zero; thread waiting to join() is notified to resume self.all_tasks_done = threading.Condition(self.mutex) self.unfinished_tasks = 0
defget(self, block=True, timeout=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 non-negative 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). ''' with self.not_empty: ifnot block: ifnot self._qsize(): raise Empty elif timeout isNone: whilenot self._qsize(): self.not_empty.wait() elif timeout < 0: raise ValueError("'timeout' must be a non-negative number") else: endtime = time() + timeout whilenot self._qsize(): remaining = endtime - time() if remaining <= 0.0: raise Empty self.not_empty.wait(remaining) item = self._get() self.not_full.notify() return item
参数为是否阻塞和超时时间
get_nowait
1 2 3 4 5 6 7
defget_nowait(self): '''Remove and return an item from the queue without blocking. Only get an item if one is immediately available. Otherwise raise the Empty exception. ''' return self.get(block=False)
非阻塞获取队列中的值
put && put_nowait
与get & get_nowait同理
empty
1 2 3 4 5 6 7 8 9 10 11 12 13
defempty(self): '''Return True if the queue is empty, False otherwise (not reliable!). This method is likely to be removed at some point. Use qsize() == 0 as a direct substitute, but be aware that either approach risks a race condition where a queue can grow before the result of empty() or qsize() can be used. To create code that needs to wait for all queued tasks to be completed, the preferred technique is to use the join() method. ''' with self.mutex: returnnot self._qsize()
检查队列是否为空,为空返回True,不为空返回False
full
1 2 3 4 5 6 7 8 9 10
deffull(self): '''Return True if the queue is full, False otherwise (not reliable!). This method is likely to be removed at some point. Use qsize() >= n as a direct substitute, but be aware that either approach risks a race condition where a queue can shrink before the result of full() or qsize() can be used. ''' with self.mutex: return0 < self.maxsize <= self._qsize()
判断队列是否已经满了
qsize
1 2 3 4
defqsize(self): '''Return the approximate size of the queue (not reliable!).''' with self.mutex: return self._qsize()