-
Notifications
You must be signed in to change notification settings - Fork 45
/
__init__.py
557 lines (444 loc) · 19.3 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
import asyncio
import threading
from asyncio import QueueEmpty as AsyncQueueEmpty
from asyncio import QueueFull as AsyncQueueFull
from collections import deque
from heapq import heappop, heappush
from queue import Empty as SyncQueueEmpty
from queue import Full as SyncQueueFull
from typing import (Generic, TypeVar, Optional, Any, Deque, List, Set,
Callable)
__version__ = '0.5.0'
__all__ = ('Queue', 'PriorityQueue', 'LifoQueue')
T = TypeVar('T')
OptInt = Optional[int]
current_loop = getattr(asyncio, 'get_running_loop', None)
if current_loop is None:
current_loop = asyncio.get_event_loop
class Queue(Generic[T]):
def __init__(self, maxsize: int = 0) -> None:
self._loop = current_loop()
self._maxsize = maxsize
self._init(maxsize)
self._unfinished_tasks = 0
self._sync_mutex = threading.Lock()
self._sync_not_empty = threading.Condition(self._sync_mutex)
self._sync_not_full = threading.Condition(self._sync_mutex)
self._all_tasks_done = threading.Condition(self._sync_mutex)
self._async_mutex = asyncio.Lock()
self._async_not_empty = asyncio.Condition(self._async_mutex)
self._async_not_full = asyncio.Condition(self._async_mutex)
self._finished = asyncio.Event()
self._finished.set()
self._closing = False
self._pending = set() # type: Set[asyncio.Future[Any]]
def checked_call_soon_threadsafe(
callback: Callable[..., None], *args: Any) -> None:
try:
self._loop.call_soon_threadsafe(callback, *args)
except RuntimeError:
# swallowing agreed in #2
pass
self._call_soon_threadsafe = checked_call_soon_threadsafe
def checked_call_soon(
callback: Callable[..., None], *args: Any) -> None:
if not self._loop.is_closed():
self._loop.call_soon(callback, *args)
self._call_soon = checked_call_soon
self._sync_queue = _SyncQueueProxy(self)
self._async_queue = _AsyncQueueProxy(self)
def close(self) -> None:
with self._sync_mutex:
self._closing = True
for fut in self._pending:
fut.cancel()
async def wait_closed(self) -> None:
# should be called from loop after close().
# Nobody should put/get at this point,
# so lock acquiring is not required
if not self._closing:
raise RuntimeError("Waiting for non-closed queue")
# give execution chances for the task-done callbacks
# of async tasks created inside
# _notify_async_not_empty, _notify_async_not_full
# methods.
await asyncio.sleep(0)
if not self._pending:
return
await asyncio.wait(self._pending)
@property
def closed(self) -> bool:
return self._closing and not self._pending
@property
def maxsize(self) -> int:
return self._maxsize
@property
def sync_q(self) -> '_SyncQueueProxy[T]':
return self._sync_queue
@property
def async_q(self) -> '_AsyncQueueProxy[T]':
return self._async_queue
# Override these methods to implement other queue organizations
# (e.g. stack or priority queue).
# These will only be called with appropriate locks held
def _init(self, maxsize: int) -> None:
self._queue = deque() # type: Deque[T]
def _qsize(self) -> int:
return len(self._queue)
# Put a new item in the queue
def _put(self, item: T) -> None:
self._queue.append(item)
# Get an item from the queue
def _get(self) -> T:
return self._queue.popleft()
def _put_internal(self, item: T) -> None:
self._put(item)
self._unfinished_tasks += 1
self._finished.clear()
def _notify_sync_not_empty(self) -> None:
def f() -> None:
with self._sync_mutex:
self._sync_not_empty.notify()
self._loop.run_in_executor(None, f)
def _notify_sync_not_full(self) -> None:
def f() -> None:
with self._sync_mutex:
self._sync_not_full.notify()
fut = self._loop.run_in_executor(None, f)
fut.add_done_callback(self._pending.discard)
self._pending.add(fut)
def _notify_async_not_empty(self, *, threadsafe: bool) -> None:
async def f() -> None:
async with self._async_mutex:
self._async_not_empty.notify()
def task_maker() -> None:
task = self._loop.create_task(f())
task.add_done_callback(self._pending.discard)
self._pending.add(task)
if threadsafe:
self._call_soon_threadsafe(task_maker)
else:
self._call_soon(task_maker)
def _notify_async_not_full(self, *, threadsafe: bool) -> None:
async def f() -> None:
async with self._async_mutex:
self._async_not_full.notify()
def task_maker() -> None:
task = self._loop.create_task(f())
task.add_done_callback(self._pending.discard)
self._pending.add(task)
if threadsafe:
self._call_soon_threadsafe(task_maker)
else:
self._call_soon(task_maker)
def _check_closing(self) -> None:
if self._closing:
raise RuntimeError('Modification of closed queue is forbidden')
class _SyncQueueProxy(Generic[T]):
'''Create a queue object with a given maximum size.
If maxsize is <= 0, the queue size is infinite.
'''
def __init__(self, parent: Queue[T]):
self._parent = parent
@property
def maxsize(self) -> int:
return self._parent._maxsize
@property
def closed(self) -> bool:
return self._parent.closed
def task_done(self) -> None:
'''Indicate that a formerly enqueued task is complete.
Used by Queue consumer threads. For each get() used to fetch a task,
a subsequent call to task_done() tells the queue that the processing
on the task is complete.
If a join() is currently blocking, it will resume when all items
have been processed (meaning that a task_done() call was received
for every item that had been put() into the queue).
Raises a ValueError if called more times than there were items
placed in the queue.
'''
self._parent._check_closing()
with self._parent._all_tasks_done:
unfinished = self._parent._unfinished_tasks - 1
if unfinished <= 0:
if unfinished < 0:
raise ValueError('task_done() called too many times')
self._parent._all_tasks_done.notify_all()
self._parent._loop.call_soon_threadsafe(
self._parent._finished.set)
self._parent._unfinished_tasks = unfinished
def join(self) -> None:
'''Blocks until all items in the Queue have been gotten and processed.
The count of unfinished tasks goes up whenever an item is added to the
queue. The count goes down whenever a consumer thread calls task_done()
to indicate the item was retrieved and all work on it is complete.
When the count of unfinished tasks drops to zero, join() unblocks.
'''
with self._parent._all_tasks_done:
while self._parent._unfinished_tasks:
self._parent._all_tasks_done.wait()
def qsize(self) -> int:
'''Return the approximate size of the queue (not reliable!).'''
return self._parent._qsize()
@property
def unfinished_tasks(self) -> int:
'''Return the number of unfinished tasks.'''
return self._parent._unfinished_tasks
def empty(self) -> bool:
'''Return True if the queue is empty, False otherwise (not reliable!).
This method is likely to be removed at some point. Use qsize() == 0
as a direct substitute, but be aware that either approach risks a race
condition where a queue can grow before the result of empty() or
qsize() can be used.
To create code that needs to wait for all queued tasks to be
completed, the preferred technique is to use the join() method.
'''
return not self._parent._qsize()
def full(self) -> bool:
'''Return True if the queue is full, False otherwise (not reliable!).
This method is likely to be removed at some point. Use qsize() >= n
as a direct substitute, but be aware that either approach risks a race
condition where a queue can shrink before the result of full() or
qsize() can be used.
'''
return 0 < self._parent._maxsize <= self._parent._qsize()
def put(self, item: T, block: bool = True,
timeout: OptInt = None) -> None:
'''Put an item into the queue.
If optional args 'block' is true and 'timeout' is None (the default),
block if necessary until a free slot is available. If 'timeout' is
a non-negative number, it blocks at most 'timeout' seconds and raises
the Full exception if no free slot was available within that time.
Otherwise ('block' is false), put an item on the queue if a free slot
is immediately available, else raise the Full exception ('timeout'
is ignored in that case).
'''
self._parent._check_closing()
with self._parent._sync_not_full:
if self._parent._maxsize > 0:
if not block:
if self._parent._qsize() >= self._parent._maxsize:
raise SyncQueueFull
elif timeout is None:
while self._parent._qsize() >= self._parent._maxsize:
self._parent._sync_not_full.wait()
elif timeout < 0:
raise ValueError("'timeout' must be a non-negative number")
else:
time = self._parent._loop.time
endtime = time() + timeout
while self._parent._qsize() >= self._parent._maxsize:
remaining = endtime - time()
if remaining <= 0.0:
raise SyncQueueFull
self._parent._sync_not_full.wait(remaining)
self._parent._put_internal(item)
self._parent._sync_not_empty.notify()
self._parent._notify_async_not_empty(threadsafe=True)
def get(self, block: bool = True, timeout: OptInt = None) -> T:
'''Remove and return an item from the queue.
If optional args 'block' is true and 'timeout' is None (the default),
block if necessary until an item is available. If 'timeout' is
a non-negative number, it blocks at most 'timeout' seconds and raises
the Empty exception if no item was available within that time.
Otherwise ('block' is false), return an item if one is immediately
available, else raise the Empty exception ('timeout' is ignored
in that case).
'''
self._parent._check_closing()
with self._parent._sync_not_empty:
if not block:
if not self._parent._qsize():
raise SyncQueueEmpty
elif timeout is None:
while not self._parent._qsize():
self._parent._sync_not_empty.wait()
elif timeout < 0:
raise ValueError("'timeout' must be a non-negative number")
else:
time = self._parent._loop.time
endtime = time() + timeout
while not self._parent._qsize():
remaining = endtime - time()
if remaining <= 0.0:
raise SyncQueueEmpty
self._parent._sync_not_empty.wait(remaining)
item = self._parent._get()
self._parent._sync_not_full.notify()
self._parent._notify_async_not_full(threadsafe=True)
return item
def put_nowait(self, item: T) -> None:
'''Put an item into the queue without blocking.
Only enqueue the item if a free slot is immediately available.
Otherwise raise the Full exception.
'''
return self.put(item, block=False)
def get_nowait(self) -> T:
'''Remove and return an item from the queue without blocking.
Only get an item if one is immediately available. Otherwise
raise the Empty exception.
'''
return self.get(block=False)
class _AsyncQueueProxy(Generic[T]):
'''Create a queue object with a given maximum size.
If maxsize is <= 0, the queue size is infinite.
'''
def __init__(self, parent: Queue[T]):
self._parent = parent
@property
def closed(self) -> bool:
return self._parent.closed
def qsize(self) -> int:
"""Number of items in the queue."""
return self._parent._qsize()
@property
def unfinished_tasks(self) -> int:
'''Return the number of unfinished tasks.'''
return self._parent._unfinished_tasks
@property
def maxsize(self) -> int:
"""Number of items allowed in the queue."""
return self._parent._maxsize
def empty(self) -> bool:
"""Return True if the queue is empty, False otherwise."""
return self.qsize() == 0
def full(self) -> bool:
"""Return True if there are maxsize items in the queue.
Note: if the Queue was initialized with maxsize=0 (the default),
then full() is never True.
"""
if self._parent._maxsize <= 0:
return False
else:
return self.qsize() >= self._parent._maxsize
async def put(self, item: T) -> None:
"""Put an item into the queue.
Put an item into the queue. If the queue is full, wait until a free
slot is available before adding item.
This method is a coroutine.
"""
self._parent._check_closing()
async with self._parent._async_not_full:
self._parent._sync_mutex.acquire()
locked = True
try:
if self._parent._maxsize > 0:
do_wait = True
while do_wait:
do_wait = (
self._parent._qsize() >= self._parent._maxsize
)
if do_wait:
locked = False
self._parent._sync_mutex.release()
await self._parent._async_not_full.wait()
self._parent._sync_mutex.acquire()
locked = True
self._parent._put_internal(item)
self._parent._async_not_empty.notify()
self._parent._notify_sync_not_empty()
finally:
if locked:
self._parent._sync_mutex.release()
def put_nowait(self, item: T) -> None:
"""Put an item into the queue without blocking.
If no free slot is immediately available, raise QueueFull.
"""
self._parent._check_closing()
with self._parent._sync_mutex:
if self._parent._maxsize > 0:
if self._parent._qsize() >= self._parent._maxsize:
raise AsyncQueueFull
self._parent._put_internal(item)
self._parent._notify_async_not_empty(threadsafe=False)
self._parent._notify_sync_not_empty()
async def get(self) -> T:
"""Remove and return an item from the queue.
If queue is empty, wait until an item is available.
This method is a coroutine.
"""
self._parent._check_closing()
async with self._parent._async_not_empty:
self._parent._sync_mutex.acquire()
locked = True
try:
do_wait = True
while do_wait:
do_wait = self._parent._qsize() == 0
if do_wait:
locked = False
self._parent._sync_mutex.release()
await self._parent._async_not_empty.wait()
self._parent._sync_mutex.acquire()
locked = True
item = self._parent._get()
self._parent._async_not_full.notify()
self._parent._notify_sync_not_full()
return item
finally:
if locked:
self._parent._sync_mutex.release()
def get_nowait(self) -> T:
"""Remove and return an item from the queue.
Return an item if one is immediately available, else raise QueueEmpty.
"""
self._parent._check_closing()
with self._parent._sync_mutex:
if self._parent._qsize() == 0:
raise AsyncQueueEmpty
item = self._parent._get()
self._parent._notify_async_not_full(threadsafe=False)
self._parent._notify_sync_not_full()
return item
def task_done(self) -> None:
"""Indicate that a formerly enqueued task is complete.
Used by queue consumers. For each get() used to fetch a task,
a subsequent call to task_done() tells the queue that the processing
on the task is complete.
If a join() is currently blocking, it will resume when all items have
been processed (meaning that a task_done() call was received for every
item that had been put() into the queue).
Raises ValueError if called more times than there were items placed in
the queue.
"""
self._parent._check_closing()
with self._parent._all_tasks_done:
if self._parent._unfinished_tasks <= 0:
raise ValueError('task_done() called too many times')
self._parent._unfinished_tasks -= 1
if self._parent._unfinished_tasks == 0:
self._parent._finished.set()
self._parent._all_tasks_done.notify_all()
async def join(self) -> None:
"""Block until all items in the queue have been gotten and processed.
The count of unfinished tasks goes up whenever an item is added to the
queue. The count goes down whenever a consumer calls task_done() to
indicate that the item was retrieved and all work on it is complete.
When the count of unfinished tasks drops to zero, join() unblocks.
"""
while True:
with self._parent._sync_mutex:
if self._parent._unfinished_tasks == 0:
break
await self._parent._finished.wait()
class PriorityQueue(Queue[T]):
'''Variant of Queue that retrieves open entries in priority order
(lowest first).
Entries are typically tuples of the form: (priority number, data).
'''
def _init(self, maxsize: int) -> None:
self._heap_queue = [] # type: List[T]
def _qsize(self) -> int:
return len(self._heap_queue)
def _put(self, item: T) -> None:
heappush(self._heap_queue, item)
def _get(self) -> T:
return heappop(self._heap_queue)
class LifoQueue(Queue[T]):
'''Variant of Queue that retrieves most recently added entries first.'''
def _qsize(self) -> int:
return len(self._queue)
def _put(self, item: T) -> None:
self._queue.append(item)
def _get(self) -> T:
return self._queue.pop()