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