|
1 | 1 | importatexit |
| 2 | +importlogging |
2 | 3 | importmultiprocessing |
3 | 4 | importqueue |
4 | 5 | fromcollectionsimportdefaultdict,deque |
|
18 | 19 |
|
19 | 20 | TESTING=False |
20 | 21 |
|
| 22 | +log=logging.getLogger(__name__) |
| 23 | + |
21 | 24 |
|
22 | 25 | classUserProcess: |
23 | 26 | def__init__(self): |
@@ -87,7 +90,7 @@ def _await_result(self): |
87 | 90 | assert (resultisNone)==self.fresh_process |
88 | 91 | exceptqueue.Empty: |
89 | 92 | alive=self.process.is_alive() |
90 | | -print(f"Process{alive=}") |
| 93 | +log.info(f"Process{alive=}") |
91 | 94 | ifalive: |
92 | 95 | self.process.terminate() |
93 | 96 | self.start_process() |
@@ -120,15 +123,15 @@ def monitor_processes(): |
120 | 123 | sleep(MONITOR.SLEEP_TIME) |
121 | 124 | percent=psutil.virtual_memory().percent |
122 | 125 | history.append(percent) |
123 | | -print(f"Recent memory usage:{history}") |
124 | | -print(f"Number of user processes:{len(user_processes)}") |
| 126 | +log.info(f"Recent memory usage:{history}") |
| 127 | +log.info(f"Number of user processes:{len(user_processes)}") |
125 | 128 | if ( |
126 | 129 | len(history)==history.maxlen |
127 | 130 | andmin(history)>MONITOR.THRESHOLD |
128 | 131 | andlen(user_processes)>MONITOR.MIN_PROCESSES |
129 | 132 | ): |
130 | 133 | oldest=min(user_processes.values(),key=lambdap:p.last_used) |
131 | | -print(f"Terminating process last used{int(time()-oldest.last_used)} seconds ago") |
| 134 | +log.info(f"Terminating process last used{int(time()-oldest.last_used)} seconds ago") |
132 | 135 | deluser_processes[oldest.user_id] |
133 | 136 | oldest.close() |
134 | 137 | history.clear() |
|