Add option to launch processes with HIGH priority

This commit is contained in:
2Shirt 2022-02-21 17:44:23 -07:00
parent 9672ad0175
commit f3522e42ef
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C

View file

@ -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(