From cd934658714e4da510d20e38f66ab98c29e5ec54 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 24 May 2025 13:12:43 +0530 Subject: [PATCH] DRYer --- src/calibre/utils/ipc/launch.py | 14 +++++++++----- src/calibre/utils/ipc/simple_worker.py | 8 ++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/calibre/utils/ipc/launch.py b/src/calibre/utils/ipc/launch.py index 1070917915..b590c81931 100644 --- a/src/calibre/utils/ipc/launch.py +++ b/src/calibre/utils/ipc/launch.py @@ -75,6 +75,14 @@ def headless_exe_path(exe_name='calibre-parallel'): return exe_path(exe_name) +def windows_creationflags_for_worker_process(priority: str = 'normal') -> int: + return { + 'high' : subprocess.HIGH_PRIORITY_CLASS, + 'normal': subprocess.NORMAL_PRIORITY_CLASS, + 'low' : subprocess.IDLE_PRIORITY_CLASS + }[priority] | subprocess.CREATE_NO_WINDOW + + class Worker: ''' Platform independent object for launching child processes. All processes @@ -182,11 +190,7 @@ class Worker: 'cwd': _cwd, } if iswindows: - priority = { - 'high' : subprocess.HIGH_PRIORITY_CLASS, - 'normal': subprocess.NORMAL_PRIORITY_CLASS, - 'low' : subprocess.IDLE_PRIORITY_CLASS}[priority] - args['creationflags'] = subprocess.CREATE_NO_WINDOW|priority + args['creationflags'] = windows_creationflags_for_worker_process(priority) else: niceness = { 'normal': 0, diff --git a/src/calibre/utils/ipc/simple_worker.py b/src/calibre/utils/ipc/simple_worker.py index bd7ce01194..bbe8c8a4d9 100644 --- a/src/calibre/utils/ipc/simple_worker.py +++ b/src/calibre/utils/ipc/simple_worker.py @@ -14,7 +14,7 @@ from threading import Thread from calibre.constants import iswindows from calibre.utils.ipc import eintr_retry_call -from calibre.utils.ipc.launch import Worker +from calibre.utils.ipc.launch import Worker, windows_creationflags_for_worker_process from calibre.utils.monotonic import monotonic from polyglot.builtins import environ_item, string_or_bytes @@ -142,11 +142,7 @@ def start_pipe_worker(command, env=None, priority='normal', **process_args): pass_fds = None try: if iswindows: - priority = { - 'high' : subprocess.HIGH_PRIORITY_CLASS, - 'normal': subprocess.NORMAL_PRIORITY_CLASS, - 'low' : subprocess.IDLE_PRIORITY_CLASS}[priority] - args['creationflags'] = subprocess.CREATE_NO_WINDOW|priority + args['creationflags'] = windows_creationflags_for_worker_process(priority) pass_fds = args.pop('pass_fds', None) if pass_fds: for fd in pass_fds: