-
Notifications
You must be signed in to change notification settings - Fork 12
/
concurrent.py
63 lines (50 loc) · 1.5 KB
/
concurrent.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
# Simple to use multi-threaded concurrent executor.
# Similar to what GNU parallel does.
from queue import Queue
from typing import Callable, Iterator, TypeVar
from threading import Thread
T, S = TypeVar("T"), TypeVar("S")
def concurrent(
fn: Callable[[T], S],
args: Iterator[T],
max_threads: int,
) -> Iterator[S]:
"""
Run a function concurrently on a list of arguments.
"""
end_marker = object()
input_q = Queue(maxsize=1)
output_q = Queue(maxsize=1)
def reader():
for arg in args:
input_q.put(arg)
for _ in range(max_threads):
input_q.put(end_marker)
reader_thread = Thread(target=reader, daemon=True)
reader_thread.start()
def worker():
while True:
arg: T | end_marker = input_q.get()
if arg is end_marker:
return
try:
result = fn(arg)
except BaseException as e:
result = e
output_q.put(result)
worker_threads = [Thread(target=worker, daemon=True) for _ in range(max_threads)]
for t in worker_threads:
t.start()
def waiter():
for t in worker_threads:
t.join()
output_q.put(end_marker)
waiter_thread = Thread(target=waiter, daemon=True)
waiter_thread.start()
while True:
v: S | BaseException | end_marker = output_q.get()
if v is end_marker:
return
if isinstance(v, BaseException):
raise v
yield v