mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
Minor code clean-ups and beautifications.
This commit is contained in:
parent
f2840a8890
commit
143f51ade5
1 changed files with 17 additions and 17 deletions
34
Lib/queue.py
34
Lib/queue.py
|
@ -1,12 +1,12 @@
|
||||||
"""A multi-producer, multi-consumer queue."""
|
"""A multi-producer, multi-consumer queue."""
|
||||||
|
|
||||||
from time import time as _time
|
|
||||||
try:
|
try:
|
||||||
import threading as _threading
|
import threading
|
||||||
except ImportError:
|
except ImportError:
|
||||||
import dummy_threading as _threading
|
import dummythreading as threading
|
||||||
from collections import deque
|
from collections import deque
|
||||||
import heapq
|
from heapq import heappush, heappop
|
||||||
|
from time import time
|
||||||
|
|
||||||
__all__ = ['Empty', 'Full', 'Queue', 'PriorityQueue', 'LifoQueue']
|
__all__ = ['Empty', 'Full', 'Queue', 'PriorityQueue', 'LifoQueue']
|
||||||
|
|
||||||
|
@ -31,19 +31,19 @@ class Queue:
|
||||||
# that acquire mutex must release it before returning. mutex
|
# that acquire mutex must release it before returning. mutex
|
||||||
# is shared between the three conditions, so acquiring and
|
# is shared between the three conditions, so acquiring and
|
||||||
# releasing the conditions also acquires and releases mutex.
|
# releasing the conditions also acquires and releases mutex.
|
||||||
self.mutex = _threading.Lock()
|
self.mutex = threading.Lock()
|
||||||
|
|
||||||
# Notify not_empty whenever an item is added to the queue; a
|
# Notify not_empty whenever an item is added to the queue; a
|
||||||
# thread waiting to get is notified then.
|
# thread waiting to get is notified then.
|
||||||
self.not_empty = _threading.Condition(self.mutex)
|
self.not_empty = threading.Condition(self.mutex)
|
||||||
|
|
||||||
# Notify not_full whenever an item is removed from the queue;
|
# Notify not_full whenever an item is removed from the queue;
|
||||||
# a thread waiting to put is notified then.
|
# a thread waiting to put is notified then.
|
||||||
self.not_full = _threading.Condition(self.mutex)
|
self.not_full = threading.Condition(self.mutex)
|
||||||
|
|
||||||
# Notify all_tasks_done whenever the number of unfinished tasks
|
# Notify all_tasks_done whenever the number of unfinished tasks
|
||||||
# drops to zero; thread waiting to join() is notified to resume
|
# drops to zero; thread waiting to join() is notified to resume
|
||||||
self.all_tasks_done = _threading.Condition(self.mutex)
|
self.all_tasks_done = threading.Condition(self.mutex)
|
||||||
self.unfinished_tasks = 0
|
self.unfinished_tasks = 0
|
||||||
|
|
||||||
def task_done(self):
|
def task_done(self):
|
||||||
|
@ -135,9 +135,9 @@ class Queue:
|
||||||
elif timeout < 0:
|
elif timeout < 0:
|
||||||
raise ValueError("'timeout' must be a positive number")
|
raise ValueError("'timeout' must be a positive number")
|
||||||
else:
|
else:
|
||||||
endtime = _time() + timeout
|
endtime = time() + timeout
|
||||||
while self._qsize() >= self.maxsize:
|
while self._qsize() >= self.maxsize:
|
||||||
remaining = endtime - _time()
|
remaining = endtime - time()
|
||||||
if remaining <= 0.0:
|
if remaining <= 0.0:
|
||||||
raise Full
|
raise Full
|
||||||
self.not_full.wait(remaining)
|
self.not_full.wait(remaining)
|
||||||
|
@ -174,9 +174,9 @@ class Queue:
|
||||||
elif timeout < 0:
|
elif timeout < 0:
|
||||||
raise ValueError("'timeout' must be a positive number")
|
raise ValueError("'timeout' must be a positive number")
|
||||||
else:
|
else:
|
||||||
endtime = _time() + timeout
|
endtime = time() + timeout
|
||||||
while not self._qsize():
|
while not self._qsize():
|
||||||
remaining = endtime - _time()
|
remaining = endtime - time()
|
||||||
if remaining <= 0.0:
|
if remaining <= 0.0:
|
||||||
raise Empty
|
raise Empty
|
||||||
self.not_empty.wait(remaining)
|
self.not_empty.wait(remaining)
|
||||||
|
@ -200,7 +200,7 @@ class Queue:
|
||||||
def _init(self, maxsize):
|
def _init(self, maxsize):
|
||||||
self.queue = deque()
|
self.queue = deque()
|
||||||
|
|
||||||
def _qsize(self, len=len):
|
def _qsize(self):
|
||||||
return len(self.queue)
|
return len(self.queue)
|
||||||
|
|
||||||
# Put a new item in the queue
|
# Put a new item in the queue
|
||||||
|
@ -221,13 +221,13 @@ class PriorityQueue(Queue):
|
||||||
def _init(self, maxsize):
|
def _init(self, maxsize):
|
||||||
self.queue = []
|
self.queue = []
|
||||||
|
|
||||||
def _qsize(self, len=len):
|
def _qsize(self):
|
||||||
return len(self.queue)
|
return len(self.queue)
|
||||||
|
|
||||||
def _put(self, item, heappush=heapq.heappush):
|
def _put(self, item):
|
||||||
heappush(self.queue, item)
|
heappush(self.queue, item)
|
||||||
|
|
||||||
def _get(self, heappop=heapq.heappop):
|
def _get(self):
|
||||||
return heappop(self.queue)
|
return heappop(self.queue)
|
||||||
|
|
||||||
|
|
||||||
|
@ -237,7 +237,7 @@ class LifoQueue(Queue):
|
||||||
def _init(self, maxsize):
|
def _init(self, maxsize):
|
||||||
self.queue = []
|
self.queue = []
|
||||||
|
|
||||||
def _qsize(self, len=len):
|
def _qsize(self):
|
||||||
return len(self.queue)
|
return len(self.queue)
|
||||||
|
|
||||||
def _put(self, item):
|
def _put(self, item):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue