From 96920981e04bac04cefeb357b61dd66ec457da9b Mon Sep 17 00:00:00 2001 From: Vizonex Date: Fri, 17 Oct 2025 13:57:29 -0500 Subject: [PATCH 1/2] make Process joining less likely to block --- aiomultiprocess/core.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/aiomultiprocess/core.py b/aiomultiprocess/core.py index 5bd13b0..77f45c0 100644 --- a/aiomultiprocess/core.py +++ b/aiomultiprocess/core.py @@ -9,7 +9,6 @@ import os import sys from typing import Any, Callable, Dict, Optional, Sequence - from .types import Context, R, Unit DEFAULT_START_METHOD = "spawn" @@ -114,7 +113,7 @@ def __init__( initargs=initargs, loop_initializer=loop_initializer, ) - self.aio_process = context.Process( # type: ignore[attr-defined] + self.aio_process: multiprocessing.Process = context.Process( # type: ignore[attr-defined] group=group, target=process_target or Process.run_async, args=(self.unit,), @@ -159,12 +158,13 @@ async def join(self, timeout: Optional[int] = None) -> None: """Wait for the process to finish execution without blocking the main thread.""" if not self.is_alive() and self.exitcode is None: raise ValueError("must start process before joining it") + + # TODO: Something more fine-tuned or find a way to hook + # a callback to signal to indicate that the process finished. + return await asyncio.to_thread(self.aio_process.join, timeout) - if timeout is not None: - return await asyncio.wait_for(self.join(), timeout) - while self.exitcode is None: - await asyncio.sleep(0.005) + @property def name(self) -> str: From 7d69b90c0116538ff8a727b21f0da7844903a706 Mon Sep 17 00:00:00 2001 From: Vizonex Date: Sat, 18 Oct 2025 20:34:06 -0500 Subject: [PATCH 2/2] implement asyncio.to_thread() in self.aio_process.join for 0.005 seconds --- aiomultiprocess/core.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/aiomultiprocess/core.py b/aiomultiprocess/core.py index 77f45c0..70ebf5c 100644 --- a/aiomultiprocess/core.py +++ b/aiomultiprocess/core.py @@ -158,10 +158,12 @@ async def join(self, timeout: Optional[int] = None) -> None: """Wait for the process to finish execution without blocking the main thread.""" if not self.is_alive() and self.exitcode is None: raise ValueError("must start process before joining it") + + if timeout: + return await asyncio.wait_for(self.join, timeout) - # TODO: Something more fine-tuned or find a way to hook - # a callback to signal to indicate that the process finished. - return await asyncio.to_thread(self.aio_process.join, timeout) + while self.exitcode is None: + await asyncio.to_thread(self.aio_process.join, 0.005)