forked from martok/intransidice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pqueue.py
62 lines (49 loc) · 1.6 KB
/
pqueue.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
from multiprocessing import Pool, Queue, cpu_count
from threading import RLock
from typing import Callable
class PQueue:
def __init__(task: Callable):
self._lock = RLock()
self._active = 0
self._done = False
self._q_task = Queue()
self._q_result = Queue()
self._pool = None
self._task = task
def push(item):
with self._lock:
self._q_task.push(item)
def generate():
self._q_result.clear()
self._done = False
self._active = 0
cpu = cpu_count()
self._pool = Pool(cpu)
def run_tasks():
while not self._done:
t = None
with self._lock:
try:
t = self._q_task.get_nowait()
except Queue.Empty:
t = None
if t is not None:
self._active += 1
if t is not None:
for r in self._task(t):
self._q_result.put(r, 100)
with self._lock:
self._active -= 1
for _ in range(cpu):
self._pool.apply_async(run_tasks, ())
while not self._done:
with self._lock:
if not self._active and self._q_task.empty():
self._done = True
while not self._q_result.empty():
r = self._q_result.get(True, 100)
if r is not None:
yield r
self._pool.join()
self._pool.close()
self._pool = None