Support for older python

This commit is contained in:
Kovid Goyal 2025-04-06 13:38:01 +05:30
parent 19c272633d
commit e34681b159
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -9,12 +9,20 @@ import ssl
import traceback import traceback
from collections.abc import Callable, Iterator from collections.abc import Callable, Iterator
from contextlib import ExitStack from contextlib import ExitStack
from itertools import batched, chain from itertools import chain, islice
from typing import Any, BinaryIO, NamedTuple, TypeVar from typing import Any, BinaryIO, NamedTuple, TypeVar
T = TypeVar('T') T = TypeVar('T')
R = TypeVar('R') 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): class _RemoteTraceback(Exception):
def __init__(self, tb): def __init__(self, tb):
@ -60,7 +68,9 @@ class Worker:
return self return self
def __exit__(self, exc_type, exc_value, tb) -> None: def __exit__(self, exc_type, exc_value, tb) -> None:
try:
self.pipe.close() self.pipe.close()
finally:
pid, status = os.waitpid(self.pid, os.WNOHANG) pid, status = os.waitpid(self.pid, os.WNOHANG)
if not pid: if not pid:
os.kill(self.pid, signal.SIGKILL) os.kill(self.pid, signal.SIGKILL)