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