-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathbackground.py
More file actions
46 lines (27 loc) · 722 Bytes
/
background.py
File metadata and controls
46 lines (27 loc) · 722 Bytes
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
#!/usr/bin/env python
import multiprocessing
import concurrent.futures
def default_n():
return multiprocessing.cpu_count()
n = default_n()
pool = concurrent.futures.ThreadPoolExecutor(max_workers=n)
callbacks = []
results = []
def run(f, *args, **kwargs):
pool._max_workers = n
pool._adjust_thread_count()
f = pool.submit(f, *args, **kwargs)
results.append(f)
return f
def task(f):
def do_task(*args, **kwargs):
result = run(f, *args, **kwargs)
for cb in callbacks:
result.add_done_callback(cb)
return result
return do_task
def callback(f):
callbacks.append(f)
def register_callback():
f()
return register_callback