Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python3..../asyncio
File: queues.py
"""Queues"""
[0] Fix | Delete
[1] Fix | Delete
__all__ = ['Queue', 'PriorityQueue', 'LifoQueue', 'QueueFull', 'QueueEmpty']
[2] Fix | Delete
[3] Fix | Delete
import collections
[4] Fix | Delete
import heapq
[5] Fix | Delete
[6] Fix | Delete
from . import compat
[7] Fix | Delete
from . import events
[8] Fix | Delete
from . import locks
[9] Fix | Delete
from .coroutines import coroutine
[10] Fix | Delete
[11] Fix | Delete
[12] Fix | Delete
class QueueEmpty(Exception):
[13] Fix | Delete
"""Exception raised when Queue.get_nowait() is called on a Queue object
[14] Fix | Delete
which is empty.
[15] Fix | Delete
"""
[16] Fix | Delete
pass
[17] Fix | Delete
[18] Fix | Delete
[19] Fix | Delete
class QueueFull(Exception):
[20] Fix | Delete
"""Exception raised when the Queue.put_nowait() method is called on a Queue
[21] Fix | Delete
object which is full.
[22] Fix | Delete
"""
[23] Fix | Delete
pass
[24] Fix | Delete
[25] Fix | Delete
[26] Fix | Delete
class Queue:
[27] Fix | Delete
"""A queue, useful for coordinating producer and consumer coroutines.
[28] Fix | Delete
[29] Fix | Delete
If maxsize is less than or equal to zero, the queue size is infinite. If it
[30] Fix | Delete
is an integer greater than 0, then "yield from put()" will block when the
[31] Fix | Delete
queue reaches maxsize, until an item is removed by get().
[32] Fix | Delete
[33] Fix | Delete
Unlike the standard library Queue, you can reliably know this Queue's size
[34] Fix | Delete
with qsize(), since your single-threaded asyncio application won't be
[35] Fix | Delete
interrupted between calling qsize() and doing an operation on the Queue.
[36] Fix | Delete
"""
[37] Fix | Delete
[38] Fix | Delete
def __init__(self, maxsize=0, *, loop=None):
[39] Fix | Delete
if loop is None:
[40] Fix | Delete
self._loop = events.get_event_loop()
[41] Fix | Delete
else:
[42] Fix | Delete
self._loop = loop
[43] Fix | Delete
self._maxsize = maxsize
[44] Fix | Delete
[45] Fix | Delete
# Futures.
[46] Fix | Delete
self._getters = collections.deque()
[47] Fix | Delete
# Futures.
[48] Fix | Delete
self._putters = collections.deque()
[49] Fix | Delete
self._unfinished_tasks = 0
[50] Fix | Delete
self._finished = locks.Event(loop=self._loop)
[51] Fix | Delete
self._finished.set()
[52] Fix | Delete
self._init(maxsize)
[53] Fix | Delete
[54] Fix | Delete
# These three are overridable in subclasses.
[55] Fix | Delete
[56] Fix | Delete
def _init(self, maxsize):
[57] Fix | Delete
self._queue = collections.deque()
[58] Fix | Delete
[59] Fix | Delete
def _get(self):
[60] Fix | Delete
return self._queue.popleft()
[61] Fix | Delete
[62] Fix | Delete
def _put(self, item):
[63] Fix | Delete
self._queue.append(item)
[64] Fix | Delete
[65] Fix | Delete
# End of the overridable methods.
[66] Fix | Delete
[67] Fix | Delete
def _wakeup_next(self, waiters):
[68] Fix | Delete
# Wake up the next waiter (if any) that isn't cancelled.
[69] Fix | Delete
while waiters:
[70] Fix | Delete
waiter = waiters.popleft()
[71] Fix | Delete
if not waiter.done():
[72] Fix | Delete
waiter.set_result(None)
[73] Fix | Delete
break
[74] Fix | Delete
[75] Fix | Delete
def __repr__(self):
[76] Fix | Delete
return '<{} at {:#x} {}>'.format(
[77] Fix | Delete
type(self).__name__, id(self), self._format())
[78] Fix | Delete
[79] Fix | Delete
def __str__(self):
[80] Fix | Delete
return '<{} {}>'.format(type(self).__name__, self._format())
[81] Fix | Delete
[82] Fix | Delete
def _format(self):
[83] Fix | Delete
result = 'maxsize={!r}'.format(self._maxsize)
[84] Fix | Delete
if getattr(self, '_queue', None):
[85] Fix | Delete
result += ' _queue={!r}'.format(list(self._queue))
[86] Fix | Delete
if self._getters:
[87] Fix | Delete
result += ' _getters[{}]'.format(len(self._getters))
[88] Fix | Delete
if self._putters:
[89] Fix | Delete
result += ' _putters[{}]'.format(len(self._putters))
[90] Fix | Delete
if self._unfinished_tasks:
[91] Fix | Delete
result += ' tasks={}'.format(self._unfinished_tasks)
[92] Fix | Delete
return result
[93] Fix | Delete
[94] Fix | Delete
def qsize(self):
[95] Fix | Delete
"""Number of items in the queue."""
[96] Fix | Delete
return len(self._queue)
[97] Fix | Delete
[98] Fix | Delete
@property
[99] Fix | Delete
def maxsize(self):
[100] Fix | Delete
"""Number of items allowed in the queue."""
[101] Fix | Delete
return self._maxsize
[102] Fix | Delete
[103] Fix | Delete
def empty(self):
[104] Fix | Delete
"""Return True if the queue is empty, False otherwise."""
[105] Fix | Delete
return not self._queue
[106] Fix | Delete
[107] Fix | Delete
def full(self):
[108] Fix | Delete
"""Return True if there are maxsize items in the queue.
[109] Fix | Delete
[110] Fix | Delete
Note: if the Queue was initialized with maxsize=0 (the default),
[111] Fix | Delete
then full() is never True.
[112] Fix | Delete
"""
[113] Fix | Delete
if self._maxsize <= 0:
[114] Fix | Delete
return False
[115] Fix | Delete
else:
[116] Fix | Delete
return self.qsize() >= self._maxsize
[117] Fix | Delete
[118] Fix | Delete
@coroutine
[119] Fix | Delete
def put(self, item):
[120] Fix | Delete
"""Put an item into the queue.
[121] Fix | Delete
[122] Fix | Delete
Put an item into the queue. If the queue is full, wait until a free
[123] Fix | Delete
slot is available before adding item.
[124] Fix | Delete
[125] Fix | Delete
This method is a coroutine.
[126] Fix | Delete
"""
[127] Fix | Delete
while self.full():
[128] Fix | Delete
putter = self._loop.create_future()
[129] Fix | Delete
self._putters.append(putter)
[130] Fix | Delete
try:
[131] Fix | Delete
yield from putter
[132] Fix | Delete
except:
[133] Fix | Delete
putter.cancel() # Just in case putter is not done yet.
[134] Fix | Delete
if not self.full() and not putter.cancelled():
[135] Fix | Delete
# We were woken up by get_nowait(), but can't take
[136] Fix | Delete
# the call. Wake up the next in line.
[137] Fix | Delete
self._wakeup_next(self._putters)
[138] Fix | Delete
raise
[139] Fix | Delete
return self.put_nowait(item)
[140] Fix | Delete
[141] Fix | Delete
def put_nowait(self, item):
[142] Fix | Delete
"""Put an item into the queue without blocking.
[143] Fix | Delete
[144] Fix | Delete
If no free slot is immediately available, raise QueueFull.
[145] Fix | Delete
"""
[146] Fix | Delete
if self.full():
[147] Fix | Delete
raise QueueFull
[148] Fix | Delete
self._put(item)
[149] Fix | Delete
self._unfinished_tasks += 1
[150] Fix | Delete
self._finished.clear()
[151] Fix | Delete
self._wakeup_next(self._getters)
[152] Fix | Delete
[153] Fix | Delete
@coroutine
[154] Fix | Delete
def get(self):
[155] Fix | Delete
"""Remove and return an item from the queue.
[156] Fix | Delete
[157] Fix | Delete
If queue is empty, wait until an item is available.
[158] Fix | Delete
[159] Fix | Delete
This method is a coroutine.
[160] Fix | Delete
"""
[161] Fix | Delete
while self.empty():
[162] Fix | Delete
getter = self._loop.create_future()
[163] Fix | Delete
self._getters.append(getter)
[164] Fix | Delete
try:
[165] Fix | Delete
yield from getter
[166] Fix | Delete
except:
[167] Fix | Delete
getter.cancel() # Just in case getter is not done yet.
[168] Fix | Delete
[169] Fix | Delete
try:
[170] Fix | Delete
self._getters.remove(getter)
[171] Fix | Delete
except ValueError:
[172] Fix | Delete
pass
[173] Fix | Delete
[174] Fix | Delete
if not self.empty() and not getter.cancelled():
[175] Fix | Delete
# We were woken up by put_nowait(), but can't take
[176] Fix | Delete
# the call. Wake up the next in line.
[177] Fix | Delete
self._wakeup_next(self._getters)
[178] Fix | Delete
raise
[179] Fix | Delete
return self.get_nowait()
[180] Fix | Delete
[181] Fix | Delete
def get_nowait(self):
[182] Fix | Delete
"""Remove and return an item from the queue.
[183] Fix | Delete
[184] Fix | Delete
Return an item if one is immediately available, else raise QueueEmpty.
[185] Fix | Delete
"""
[186] Fix | Delete
if self.empty():
[187] Fix | Delete
raise QueueEmpty
[188] Fix | Delete
item = self._get()
[189] Fix | Delete
self._wakeup_next(self._putters)
[190] Fix | Delete
return item
[191] Fix | Delete
[192] Fix | Delete
def task_done(self):
[193] Fix | Delete
"""Indicate that a formerly enqueued task is complete.
[194] Fix | Delete
[195] Fix | Delete
Used by queue consumers. For each get() used to fetch a task,
[196] Fix | Delete
a subsequent call to task_done() tells the queue that the processing
[197] Fix | Delete
on the task is complete.
[198] Fix | Delete
[199] Fix | Delete
If a join() is currently blocking, it will resume when all items have
[200] Fix | Delete
been processed (meaning that a task_done() call was received for every
[201] Fix | Delete
item that had been put() into the queue).
[202] Fix | Delete
[203] Fix | Delete
Raises ValueError if called more times than there were items placed in
[204] Fix | Delete
the queue.
[205] Fix | Delete
"""
[206] Fix | Delete
if self._unfinished_tasks <= 0:
[207] Fix | Delete
raise ValueError('task_done() called too many times')
[208] Fix | Delete
self._unfinished_tasks -= 1
[209] Fix | Delete
if self._unfinished_tasks == 0:
[210] Fix | Delete
self._finished.set()
[211] Fix | Delete
[212] Fix | Delete
@coroutine
[213] Fix | Delete
def join(self):
[214] Fix | Delete
"""Block until all items in the queue have been gotten and processed.
[215] Fix | Delete
[216] Fix | Delete
The count of unfinished tasks goes up whenever an item is added to the
[217] Fix | Delete
queue. The count goes down whenever a consumer calls task_done() to
[218] Fix | Delete
indicate that the item was retrieved and all work on it is complete.
[219] Fix | Delete
When the count of unfinished tasks drops to zero, join() unblocks.
[220] Fix | Delete
"""
[221] Fix | Delete
if self._unfinished_tasks > 0:
[222] Fix | Delete
yield from self._finished.wait()
[223] Fix | Delete
[224] Fix | Delete
[225] Fix | Delete
class PriorityQueue(Queue):
[226] Fix | Delete
"""A subclass of Queue; retrieves entries in priority order (lowest first).
[227] Fix | Delete
[228] Fix | Delete
Entries are typically tuples of the form: (priority number, data).
[229] Fix | Delete
"""
[230] Fix | Delete
[231] Fix | Delete
def _init(self, maxsize):
[232] Fix | Delete
self._queue = []
[233] Fix | Delete
[234] Fix | Delete
def _put(self, item, heappush=heapq.heappush):
[235] Fix | Delete
heappush(self._queue, item)
[236] Fix | Delete
[237] Fix | Delete
def _get(self, heappop=heapq.heappop):
[238] Fix | Delete
return heappop(self._queue)
[239] Fix | Delete
[240] Fix | Delete
[241] Fix | Delete
class LifoQueue(Queue):
[242] Fix | Delete
"""A subclass of Queue that retrieves most recently added entries first."""
[243] Fix | Delete
[244] Fix | Delete
def _init(self, maxsize):
[245] Fix | Delete
self._queue = []
[246] Fix | Delete
[247] Fix | Delete
def _put(self, item):
[248] Fix | Delete
self._queue.append(item)
[249] Fix | Delete
[250] Fix | Delete
def _get(self):
[251] Fix | Delete
return self._queue.pop()
[252] Fix | Delete
[253] Fix | Delete
[254] Fix | Delete
if not compat.PY35:
[255] Fix | Delete
JoinableQueue = Queue
[256] Fix | Delete
"""Deprecated alias for Queue."""
[257] Fix | Delete
__all__.append('JoinableQueue')
[258] Fix | Delete
[259] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function