Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/imh-pyth.../lib/python3....
File: queue.py
'''A multi-producer, multi-consumer queue.'''
[0] Fix | Delete
[1] Fix | Delete
import threading
[2] Fix | Delete
import types
[3] Fix | Delete
from collections import deque
[4] Fix | Delete
from heapq import heappush, heappop
[5] Fix | Delete
from time import monotonic as time
[6] Fix | Delete
try:
[7] Fix | Delete
from _queue import SimpleQueue
[8] Fix | Delete
except ImportError:
[9] Fix | Delete
SimpleQueue = None
[10] Fix | Delete
[11] Fix | Delete
__all__ = ['Empty', 'Full', 'Queue', 'PriorityQueue', 'LifoQueue', 'SimpleQueue']
[12] Fix | Delete
[13] Fix | Delete
[14] Fix | Delete
try:
[15] Fix | Delete
from _queue import Empty
[16] Fix | Delete
except ImportError:
[17] Fix | Delete
class Empty(Exception):
[18] Fix | Delete
'Exception raised by Queue.get(block=0)/get_nowait().'
[19] Fix | Delete
pass
[20] Fix | Delete
[21] Fix | Delete
class Full(Exception):
[22] Fix | Delete
'Exception raised by Queue.put(block=0)/put_nowait().'
[23] Fix | Delete
pass
[24] Fix | Delete
[25] Fix | Delete
[26] Fix | Delete
class Queue:
[27] Fix | Delete
'''Create a queue object with a given maximum size.
[28] Fix | Delete
[29] Fix | Delete
If maxsize is <= 0, the queue size is infinite.
[30] Fix | Delete
'''
[31] Fix | Delete
[32] Fix | Delete
def __init__(self, maxsize=0):
[33] Fix | Delete
self.maxsize = maxsize
[34] Fix | Delete
self._init(maxsize)
[35] Fix | Delete
[36] Fix | Delete
# mutex must be held whenever the queue is mutating. All methods
[37] Fix | Delete
# that acquire mutex must release it before returning. mutex
[38] Fix | Delete
# is shared between the three conditions, so acquiring and
[39] Fix | Delete
# releasing the conditions also acquires and releases mutex.
[40] Fix | Delete
self.mutex = threading.Lock()
[41] Fix | Delete
[42] Fix | Delete
# Notify not_empty whenever an item is added to the queue; a
[43] Fix | Delete
# thread waiting to get is notified then.
[44] Fix | Delete
self.not_empty = threading.Condition(self.mutex)
[45] Fix | Delete
[46] Fix | Delete
# Notify not_full whenever an item is removed from the queue;
[47] Fix | Delete
# a thread waiting to put is notified then.
[48] Fix | Delete
self.not_full = threading.Condition(self.mutex)
[49] Fix | Delete
[50] Fix | Delete
# Notify all_tasks_done whenever the number of unfinished tasks
[51] Fix | Delete
# drops to zero; thread waiting to join() is notified to resume
[52] Fix | Delete
self.all_tasks_done = threading.Condition(self.mutex)
[53] Fix | Delete
self.unfinished_tasks = 0
[54] Fix | Delete
[55] Fix | Delete
def task_done(self):
[56] Fix | Delete
'''Indicate that a formerly enqueued task is complete.
[57] Fix | Delete
[58] Fix | Delete
Used by Queue consumer threads. For each get() used to fetch a task,
[59] Fix | Delete
a subsequent call to task_done() tells the queue that the processing
[60] Fix | Delete
on the task is complete.
[61] Fix | Delete
[62] Fix | Delete
If a join() is currently blocking, it will resume when all items
[63] Fix | Delete
have been processed (meaning that a task_done() call was received
[64] Fix | Delete
for every item that had been put() into the queue).
[65] Fix | Delete
[66] Fix | Delete
Raises a ValueError if called more times than there were items
[67] Fix | Delete
placed in the queue.
[68] Fix | Delete
'''
[69] Fix | Delete
with self.all_tasks_done:
[70] Fix | Delete
unfinished = self.unfinished_tasks - 1
[71] Fix | Delete
if unfinished <= 0:
[72] Fix | Delete
if unfinished < 0:
[73] Fix | Delete
raise ValueError('task_done() called too many times')
[74] Fix | Delete
self.all_tasks_done.notify_all()
[75] Fix | Delete
self.unfinished_tasks = unfinished
[76] Fix | Delete
[77] Fix | Delete
def join(self):
[78] Fix | Delete
'''Blocks until all items in the Queue have been gotten and processed.
[79] Fix | Delete
[80] Fix | Delete
The count of unfinished tasks goes up whenever an item is added to the
[81] Fix | Delete
queue. The count goes down whenever a consumer thread calls task_done()
[82] Fix | Delete
to indicate the item was retrieved and all work on it is complete.
[83] Fix | Delete
[84] Fix | Delete
When the count of unfinished tasks drops to zero, join() unblocks.
[85] Fix | Delete
'''
[86] Fix | Delete
with self.all_tasks_done:
[87] Fix | Delete
while self.unfinished_tasks:
[88] Fix | Delete
self.all_tasks_done.wait()
[89] Fix | Delete
[90] Fix | Delete
def qsize(self):
[91] Fix | Delete
'''Return the approximate size of the queue (not reliable!).'''
[92] Fix | Delete
with self.mutex:
[93] Fix | Delete
return self._qsize()
[94] Fix | Delete
[95] Fix | Delete
def empty(self):
[96] Fix | Delete
'''Return True if the queue is empty, False otherwise (not reliable!).
[97] Fix | Delete
[98] Fix | Delete
This method is likely to be removed at some point. Use qsize() == 0
[99] Fix | Delete
as a direct substitute, but be aware that either approach risks a race
[100] Fix | Delete
condition where a queue can grow before the result of empty() or
[101] Fix | Delete
qsize() can be used.
[102] Fix | Delete
[103] Fix | Delete
To create code that needs to wait for all queued tasks to be
[104] Fix | Delete
completed, the preferred technique is to use the join() method.
[105] Fix | Delete
'''
[106] Fix | Delete
with self.mutex:
[107] Fix | Delete
return not self._qsize()
[108] Fix | Delete
[109] Fix | Delete
def full(self):
[110] Fix | Delete
'''Return True if the queue is full, False otherwise (not reliable!).
[111] Fix | Delete
[112] Fix | Delete
This method is likely to be removed at some point. Use qsize() >= n
[113] Fix | Delete
as a direct substitute, but be aware that either approach risks a race
[114] Fix | Delete
condition where a queue can shrink before the result of full() or
[115] Fix | Delete
qsize() can be used.
[116] Fix | Delete
'''
[117] Fix | Delete
with self.mutex:
[118] Fix | Delete
return 0 < self.maxsize <= self._qsize()
[119] Fix | Delete
[120] Fix | Delete
def put(self, item, block=True, timeout=None):
[121] Fix | Delete
'''Put an item into the queue.
[122] Fix | Delete
[123] Fix | Delete
If optional args 'block' is true and 'timeout' is None (the default),
[124] Fix | Delete
block if necessary until a free slot is available. If 'timeout' is
[125] Fix | Delete
a non-negative number, it blocks at most 'timeout' seconds and raises
[126] Fix | Delete
the Full exception if no free slot was available within that time.
[127] Fix | Delete
Otherwise ('block' is false), put an item on the queue if a free slot
[128] Fix | Delete
is immediately available, else raise the Full exception ('timeout'
[129] Fix | Delete
is ignored in that case).
[130] Fix | Delete
'''
[131] Fix | Delete
with self.not_full:
[132] Fix | Delete
if self.maxsize > 0:
[133] Fix | Delete
if not block:
[134] Fix | Delete
if self._qsize() >= self.maxsize:
[135] Fix | Delete
raise Full
[136] Fix | Delete
elif timeout is None:
[137] Fix | Delete
while self._qsize() >= self.maxsize:
[138] Fix | Delete
self.not_full.wait()
[139] Fix | Delete
elif timeout < 0:
[140] Fix | Delete
raise ValueError("'timeout' must be a non-negative number")
[141] Fix | Delete
else:
[142] Fix | Delete
endtime = time() + timeout
[143] Fix | Delete
while self._qsize() >= self.maxsize:
[144] Fix | Delete
remaining = endtime - time()
[145] Fix | Delete
if remaining <= 0.0:
[146] Fix | Delete
raise Full
[147] Fix | Delete
self.not_full.wait(remaining)
[148] Fix | Delete
self._put(item)
[149] Fix | Delete
self.unfinished_tasks += 1
[150] Fix | Delete
self.not_empty.notify()
[151] Fix | Delete
[152] Fix | Delete
def get(self, block=True, timeout=None):
[153] Fix | Delete
'''Remove and return an item from the queue.
[154] Fix | Delete
[155] Fix | Delete
If optional args 'block' is true and 'timeout' is None (the default),
[156] Fix | Delete
block if necessary until an item is available. If 'timeout' is
[157] Fix | Delete
a non-negative number, it blocks at most 'timeout' seconds and raises
[158] Fix | Delete
the Empty exception if no item was available within that time.
[159] Fix | Delete
Otherwise ('block' is false), return an item if one is immediately
[160] Fix | Delete
available, else raise the Empty exception ('timeout' is ignored
[161] Fix | Delete
in that case).
[162] Fix | Delete
'''
[163] Fix | Delete
with self.not_empty:
[164] Fix | Delete
if not block:
[165] Fix | Delete
if not self._qsize():
[166] Fix | Delete
raise Empty
[167] Fix | Delete
elif timeout is None:
[168] Fix | Delete
while not self._qsize():
[169] Fix | Delete
self.not_empty.wait()
[170] Fix | Delete
elif timeout < 0:
[171] Fix | Delete
raise ValueError("'timeout' must be a non-negative number")
[172] Fix | Delete
else:
[173] Fix | Delete
endtime = time() + timeout
[174] Fix | Delete
while not self._qsize():
[175] Fix | Delete
remaining = endtime - time()
[176] Fix | Delete
if remaining <= 0.0:
[177] Fix | Delete
raise Empty
[178] Fix | Delete
self.not_empty.wait(remaining)
[179] Fix | Delete
item = self._get()
[180] Fix | Delete
self.not_full.notify()
[181] Fix | Delete
return item
[182] Fix | Delete
[183] Fix | Delete
def put_nowait(self, item):
[184] Fix | Delete
'''Put an item into the queue without blocking.
[185] Fix | Delete
[186] Fix | Delete
Only enqueue the item if a free slot is immediately available.
[187] Fix | Delete
Otherwise raise the Full exception.
[188] Fix | Delete
'''
[189] Fix | Delete
return self.put(item, block=False)
[190] Fix | Delete
[191] Fix | Delete
def get_nowait(self):
[192] Fix | Delete
'''Remove and return an item from the queue without blocking.
[193] Fix | Delete
[194] Fix | Delete
Only get an item if one is immediately available. Otherwise
[195] Fix | Delete
raise the Empty exception.
[196] Fix | Delete
'''
[197] Fix | Delete
return self.get(block=False)
[198] Fix | Delete
[199] Fix | Delete
# Override these methods to implement other queue organizations
[200] Fix | Delete
# (e.g. stack or priority queue).
[201] Fix | Delete
# These will only be called with appropriate locks held
[202] Fix | Delete
[203] Fix | Delete
# Initialize the queue representation
[204] Fix | Delete
def _init(self, maxsize):
[205] Fix | Delete
self.queue = deque()
[206] Fix | Delete
[207] Fix | Delete
def _qsize(self):
[208] Fix | Delete
return len(self.queue)
[209] Fix | Delete
[210] Fix | Delete
# Put a new item in the queue
[211] Fix | Delete
def _put(self, item):
[212] Fix | Delete
self.queue.append(item)
[213] Fix | Delete
[214] Fix | Delete
# Get an item from the queue
[215] Fix | Delete
def _get(self):
[216] Fix | Delete
return self.queue.popleft()
[217] Fix | Delete
[218] Fix | Delete
__class_getitem__ = classmethod(types.GenericAlias)
[219] Fix | Delete
[220] Fix | Delete
[221] Fix | Delete
class PriorityQueue(Queue):
[222] Fix | Delete
'''Variant of Queue that retrieves open entries in priority order (lowest first).
[223] Fix | Delete
[224] Fix | Delete
Entries are typically tuples of the form: (priority number, data).
[225] Fix | Delete
'''
[226] Fix | Delete
[227] Fix | Delete
def _init(self, maxsize):
[228] Fix | Delete
self.queue = []
[229] Fix | Delete
[230] Fix | Delete
def _qsize(self):
[231] Fix | Delete
return len(self.queue)
[232] Fix | Delete
[233] Fix | Delete
def _put(self, item):
[234] Fix | Delete
heappush(self.queue, item)
[235] Fix | Delete
[236] Fix | Delete
def _get(self):
[237] Fix | Delete
return heappop(self.queue)
[238] Fix | Delete
[239] Fix | Delete
[240] Fix | Delete
class LifoQueue(Queue):
[241] Fix | Delete
'''Variant of Queue that retrieves most recently added entries first.'''
[242] Fix | Delete
[243] Fix | Delete
def _init(self, maxsize):
[244] Fix | Delete
self.queue = []
[245] Fix | Delete
[246] Fix | Delete
def _qsize(self):
[247] Fix | Delete
return len(self.queue)
[248] Fix | Delete
[249] Fix | Delete
def _put(self, item):
[250] Fix | Delete
self.queue.append(item)
[251] Fix | Delete
[252] Fix | Delete
def _get(self):
[253] Fix | Delete
return self.queue.pop()
[254] Fix | Delete
[255] Fix | Delete
[256] Fix | Delete
class _PySimpleQueue:
[257] Fix | Delete
'''Simple, unbounded FIFO queue.
[258] Fix | Delete
[259] Fix | Delete
This pure Python implementation is not reentrant.
[260] Fix | Delete
'''
[261] Fix | Delete
# Note: while this pure Python version provides fairness
[262] Fix | Delete
# (by using a threading.Semaphore which is itself fair, being based
[263] Fix | Delete
# on threading.Condition), fairness is not part of the API contract.
[264] Fix | Delete
# This allows the C version to use a different implementation.
[265] Fix | Delete
[266] Fix | Delete
def __init__(self):
[267] Fix | Delete
self._queue = deque()
[268] Fix | Delete
self._count = threading.Semaphore(0)
[269] Fix | Delete
[270] Fix | Delete
def put(self, item, block=True, timeout=None):
[271] Fix | Delete
'''Put the item on the queue.
[272] Fix | Delete
[273] Fix | Delete
The optional 'block' and 'timeout' arguments are ignored, as this method
[274] Fix | Delete
never blocks. They are provided for compatibility with the Queue class.
[275] Fix | Delete
'''
[276] Fix | Delete
self._queue.append(item)
[277] Fix | Delete
self._count.release()
[278] Fix | Delete
[279] Fix | Delete
def get(self, block=True, timeout=None):
[280] Fix | Delete
'''Remove and return an item from the queue.
[281] Fix | Delete
[282] Fix | Delete
If optional args 'block' is true and 'timeout' is None (the default),
[283] Fix | Delete
block if necessary until an item is available. If 'timeout' is
[284] Fix | Delete
a non-negative number, it blocks at most 'timeout' seconds and raises
[285] Fix | Delete
the Empty exception if no item was available within that time.
[286] Fix | Delete
Otherwise ('block' is false), return an item if one is immediately
[287] Fix | Delete
available, else raise the Empty exception ('timeout' is ignored
[288] Fix | Delete
in that case).
[289] Fix | Delete
'''
[290] Fix | Delete
if timeout is not None and timeout < 0:
[291] Fix | Delete
raise ValueError("'timeout' must be a non-negative number")
[292] Fix | Delete
if not self._count.acquire(block, timeout):
[293] Fix | Delete
raise Empty
[294] Fix | Delete
return self._queue.popleft()
[295] Fix | Delete
[296] Fix | Delete
def put_nowait(self, item):
[297] Fix | Delete
'''Put an item into the queue without blocking.
[298] Fix | Delete
[299] Fix | Delete
This is exactly equivalent to `put(item)` and is only provided
[300] Fix | Delete
for compatibility with the Queue class.
[301] Fix | Delete
'''
[302] Fix | Delete
return self.put(item, block=False)
[303] Fix | Delete
[304] Fix | Delete
def get_nowait(self):
[305] Fix | Delete
'''Remove and return an item from the queue without blocking.
[306] Fix | Delete
[307] Fix | Delete
Only get an item if one is immediately available. Otherwise
[308] Fix | Delete
raise the Empty exception.
[309] Fix | Delete
'''
[310] Fix | Delete
return self.get(block=False)
[311] Fix | Delete
[312] Fix | Delete
def empty(self):
[313] Fix | Delete
'''Return True if the queue is empty, False otherwise (not reliable!).'''
[314] Fix | Delete
return len(self._queue) == 0
[315] Fix | Delete
[316] Fix | Delete
def qsize(self):
[317] Fix | Delete
'''Return the approximate size of the queue (not reliable!).'''
[318] Fix | Delete
return len(self._queue)
[319] Fix | Delete
[320] Fix | Delete
__class_getitem__ = classmethod(types.GenericAlias)
[321] Fix | Delete
[322] Fix | Delete
[323] Fix | Delete
if SimpleQueue is None:
[324] Fix | Delete
SimpleQueue = _PySimpleQueue
[325] Fix | Delete
[326] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function