Wiki source code of ThreadPool
Last modified by John on 2021/11/29 15:04
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | {{toc-local/}} | ||
2 | |||
3 | = Introduction = | ||
4 | |||
5 | Provides an easy way to manage a pool of threads and submit and reap tasks run by those threads. | ||
6 | |||
7 | = Examples = | ||
8 | |||
9 | Generation of random numbers (known count): | ||
10 | |||
11 | {{code language="py"}} | ||
12 | import random | ||
13 | from threadpool import ThreadPool | ||
14 | |||
15 | tp = ThreadPool(5) | ||
16 | for i in range(100): | ||
17 | tp.add(i, random.random) | ||
18 | results = [tp.reap() for i in range(100)] | ||
19 | print("\n".join(map(str, results))) | ||
20 | {{/code}} | ||
21 | |||
22 | Notes: | ||
23 | |||
24 | * pool of 5 threads available to run tasks | ||
25 | * 100 tasks queued up | ||
26 | * all 100 tasks are expected and reaped | ||
27 | |||
28 | Generation of random numbers (unknown count): | ||
29 | |||
30 | {{code language="py" linenumbers="true"}} | ||
31 | import random | ||
32 | from threadpool import ThreadPool | ||
33 | |||
34 | tp = ThreadPool(5) | ||
35 | for i in range(100): | ||
36 | tp.add(i, random.random) | ||
37 | tp.drain() | ||
38 | results = [] | ||
39 | while not tp.is_empty(): | ||
40 | res = tp.reap() | ||
41 | results.append(res) | ||
42 | print("\n".join(map(str, results))) | ||
43 | {{/code}} | ||
44 | |||
45 | Notes: | ||
46 | |||
47 | * tp.drain() may interrupt the processing of tasks so that the number of completed tasks is not known | ||
48 | * tp.is_empty() checks for waiting, running, and done tasks |