From e34681b159c881c92bcf8adfaa14a4fc77bfb92e Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 6 Apr 2025 13:38:01 +0530 Subject: [PATCH] Support for older python --- src/calibre/utils/forked_map.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/calibre/utils/forked_map.py b/src/calibre/utils/forked_map.py index 8d61c4bdbb..6b530aefa2 100644 --- a/src/calibre/utils/forked_map.py +++ b/src/calibre/utils/forked_map.py @@ -9,12 +9,20 @@ import ssl import traceback from collections.abc import Callable, Iterator from contextlib import ExitStack -from itertools import batched, chain +from itertools import chain, islice from typing import Any, BinaryIO, NamedTuple, TypeVar T = TypeVar('T') R = TypeVar('R') +try: + from itertools import batched +except ImportError: + def batched(iterable, n): + iterator = iter(iterable) + while batch := tuple(islice(iterator, n)): + yield batch + class _RemoteTraceback(Exception): def __init__(self, tb): @@ -60,11 +68,13 @@ class Worker: return self def __exit__(self, exc_type, exc_value, tb) -> None: - self.pipe.close() - pid, status = os.waitpid(self.pid, os.WNOHANG) - if not pid: - os.kill(self.pid, signal.SIGKILL) - os.waitpid(self.pid, 0) + try: + self.pipe.close() + finally: + pid, status = os.waitpid(self.pid, os.WNOHANG) + if not pid: + os.kill(self.pid, signal.SIGKILL) + os.waitpid(self.pid, 0) class Result(NamedTuple):