From b2b454050567cb989ebe9795496e8ca2f01d91ed Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 25 Nov 2019 15:18:13 +0530 Subject: [PATCH] Allow subprocess to use posix_spawn() This is both faster and needed on macOS where one cannot use fork() when using Qt. --- src/calibre/utils/ipc/launch.py | 9 +-------- src/calibre/utils/ipc/simple_worker.py | 9 +-------- src/calibre/utils/ipc/worker.py | 6 ++++++ 3 files changed, 8 insertions(+), 16 deletions(-) diff --git a/src/calibre/utils/ipc/launch.py b/src/calibre/utils/ipc/launch.py index 050462bd1e..71a42a0234 100644 --- a/src/calibre/utils/ipc/launch.py +++ b/src/calibre/utils/ipc/launch.py @@ -6,7 +6,6 @@ __copyright__ = '2009, Kovid Goyal ' __docformat__ = 'restructuredtext en' import subprocess, os, sys, time -from functools import partial from calibre.constants import iswindows, isosx, isfrozen from calibre.utils.config import prefs @@ -171,7 +170,7 @@ class Worker(object): 'low' : 10, 'high' : 20, }[priority] - args['preexec_fn'] = partial(renice, niceness) + args['env']['CALIBRE_WORKER_NICENESS'] = str(niceness) ret = None if redirect_output: self._file = PersistentTemporaryFile('_worker_redirect.log') @@ -188,12 +187,6 @@ class Worker(object): args['stdout'] = windows_null_file args['stderr'] = subprocess.STDOUT - if not iswindows: - # Close inherited file descriptors in worker - # On windows, this is done in the worker process - # itself - args['close_fds'] = True - self.child = subprocess.Popen(cmd, **args) if 'stdin' in args: self.child.stdin.close() diff --git a/src/calibre/utils/ipc/simple_worker.py b/src/calibre/utils/ipc/simple_worker.py index 91076187e1..bf4618660a 100644 --- a/src/calibre/utils/ipc/simple_worker.py +++ b/src/calibre/utils/ipc/simple_worker.py @@ -143,7 +143,6 @@ def create_worker(env, priority='normal', cwd=None, func='main'): def start_pipe_worker(command, env=None, priority='normal', **process_args): import subprocess - from functools import partial w = Worker(env or {}) args = {'stdout':subprocess.PIPE, 'stdin':subprocess.PIPE, 'env':w.env} args.update(process_args) @@ -155,14 +154,8 @@ def start_pipe_worker(command, env=None, priority='normal', **process_args): 'low' : win32process.IDLE_PRIORITY_CLASS}[priority] args['creationflags'] = win32process.CREATE_NO_WINDOW|priority else: - def renice(niceness): - try: - os.nice(niceness) - except: - pass niceness = {'normal' : 0, 'low' : 10, 'high' : 20}[priority] - args['preexec_fn'] = partial(renice, niceness) - args['close_fds'] = True + args['env']['CALIBRE_WORKER_NICENESS'] = str(niceness) exe = w.executable cmd = [exe] if isinstance(exe, string_or_bytes) else exe diff --git a/src/calibre/utils/ipc/worker.py b/src/calibre/utils/ipc/worker.py index 4756ca2cc5..d10e968e62 100644 --- a/src/calibre/utils/ipc/worker.py +++ b/src/calibre/utils/ipc/worker.py @@ -177,6 +177,12 @@ def main(): # so launch the gui as usual from calibre.gui2.main import main as gui_main return gui_main(['calibre']) + niceness = os.environ.pop('CALIBRE_WORKER_NICENESS', None) + if niceness: + try: + os.nice(int(niceness)) + except Exception: + pass csw = os.environ.pop('CALIBRE_SIMPLE_WORKER', None) if csw: mod, _, func = csw.partition(':')