From f3522e42efe3c5a1206ef230974e4f5a619e41b3 Mon Sep 17 00:00:00 2001 From: 2Shirt <2xShirt@gmail.com> Date: Mon, 21 Feb 2022 17:44:23 -0700 Subject: [PATCH] Add option to launch processes with HIGH priority --- scripts/wk/exe.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/scripts/wk/exe.py b/scripts/wk/exe.py index e57240c9..73cee622 100644 --- a/scripts/wk/exe.py +++ b/scripts/wk/exe.py @@ -75,7 +75,8 @@ class NonBlockingStreamReader(): # Functions -def build_cmd_kwargs(cmd, minimized=False, pipe=True, shell=False, **kwargs): +def build_cmd_kwargs( + cmd, minimized=False, pipe=True, priority=False, shell=False, **kwargs): """Build kwargs for use by subprocess functions, returns dict. Specifically subprocess.run() and subprocess.Popen(). @@ -112,6 +113,9 @@ def build_cmd_kwargs(cmd, minimized=False, pipe=True, shell=False, **kwargs): startupinfo.wShowWindow = 6 cmd_kwargs['startupinfo'] = startupinfo + # High priority + if priority: + cmd_kwargs['creationflags'] = subprocess.HIGH_PRIORITY_CLASS # Pipe output if pipe: @@ -236,6 +240,18 @@ def run_program(cmd, check=True, pipe=True, shell=False, **kwargs): return proc +def set_proc_priority(name, priority, exact=True): + """Set process priority by name. + + NOTE: priority currently can be only set to NORMAL or HIGH. + """ + if priority not in ('NORMAL', 'HIGH'): + raise RuntimeError('Invalid process priority specified.') + + for proc in get_procs(name, exact=exact): + proc.nice(psutil.HIGH_PRIORITY_CLASS) + + def start_thread(function, args=None, daemon=True): """Run function as thread in background, returns Thread object.""" LOG.debug(