-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy paththread_pool.py
More file actions
52 lines (40 loc) · 1.49 KB
/
Copy paththread_pool.py
File metadata and controls
52 lines (40 loc) · 1.49 KB
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
import sys
import time
import threading
import espp
def main() -> None:
# ---------------------------------------------------------------------------
# Basic usage: submit jobs, wait for completion, read stats
# ---------------------------------------------------------------------------
start = time.time()
def elapsed():
return time.time() - start
counter_lock = threading.Lock()
completed = [0]
done = threading.Event()
TOTAL_JOBS = 8
pool = espp.ThreadPool(espp.ThreadPool.Config(
worker_count=2,
max_queue_size=0,
auto_start=True,
log_level=espp.Logger.Verbosity.warn,
))
print(f"[{elapsed():.3f}] Pool started, worker_count={pool.worker_count()}, is_running={pool.is_running()}")
for i in range(TOTAL_JOBS):
def _job(i=i):
time.sleep(0.2)
with counter_lock:
completed[0] += 1
print(f"[{elapsed():.3f}] Job {i} done ({completed[0]}/{TOTAL_JOBS})")
if completed[0] >= TOTAL_JOBS:
done.set()
pool.submit(_job)
print(f"[{elapsed():.3f}] All {TOTAL_JOBS} jobs submitted, queue_size={pool.queue_size()}")
done.wait()
s = pool.stats()
print(f"[{elapsed():.3f}] All jobs complete — submitted={s.submitted} executed={s.executed} rejected={s.rejected}")
pool.stop()
print(f"[{elapsed():.3f}] Pool stopped, is_running={pool.is_running()}")
if __name__ == "__main__":
main()
sys.exit(0)