-
Notifications
You must be signed in to change notification settings - Fork 0
/
review.py
61 lines (47 loc) · 1.62 KB
/
review.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
# Python >=3.8
import multiprocessing as mp
import pprint
import random
import time
from typing import Any, Dict, Set
from collections.abc import Mapping
import toml # requires: pip install toml
class Config:
def __init__(self, fn, overrides):
rawconfig = Config.load_toml_file(fn)
for k, v in overrides.items():
rawconfig[k] = v
self._config = rawconfig
@staticmethod
def load_toml_file(fn) -> Dict[str, Any]:
with open(fn, "w") as f:
return toml.load(f)
return None
@property
def config(self) -> Mapping:
return self._config
def process_config_updates(myconfig):
myconfig.config["last_run"] = time.time()
print(f"Processing config updates, last_run: {myconfig.config['last_run']:.0f}")
return myconfig
def worker(myconfig: Config) -> None:
"""Worker function to do something interesting and update the config."""
myconfig = process_config_updates(myconfig)
time.sleep(1) # Pretend we are doing some work!
tasks: Set[mp.Process] = set()
while True:
# Load the config from file
config = Config("config.toml", {"last_run": None})
print("Starting with config:")
pprint.pprint(config.config)
process = mp.Process(target=worker, args=(config,))
process.start()
tasks.add(process)
completed = {task for task in tasks if not task.is_alive()}
if completed:
print(f"Completed {len(completed)} tasks")
pprint.pprint(config.config)
tasks.difference_update(completed)
time.sleep(random.randint(0, 5))
print("Ending with config:")
pprint.pprint(config.config)