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