Skip to content

processes

Class info

🛈 DocStrings

Process ceation utilities.

create_process async

create_process(
    command: str | PathLike[str],
    *args: str | PathLike[str],
    stdin: Mode | None = None,
    stdout: Mode | None = None,
    stderr: Mode | None = None,
    limit: int = 10 * 1024 * 1024,
    env: dict[str, str] | None = None,
    cwd: str | PathLike[str] | None = None,
    start_new_session: bool = False,
) -> Process

Small create_subprocess_exec wrapper.

Source code in src/anyenv/processes.py
async def create_process(
    command: str | os.PathLike[str],
    *args: str | os.PathLike[str],
    stdin: Mode | None = None,
    stdout: Mode | None = None,
    stderr: Mode | None = None,
    limit: int = 10 * 1024 * 1024,
    env: dict[str, str] | None = None,
    cwd: str | os.PathLike[str] | None = None,
    start_new_session: bool = False,
) -> Process:
    """Small create_subprocess_exec wrapper."""
    return await asyncio.create_subprocess_exec(
        command,
        *args,
        stdin=MAP[stdin] if stdin else None,
        stdout=MAP[stdout] if stdout else None,
        stderr=MAP[stderr] if stderr else None,
        limit=limit,
        cwd=cwd,
        env=env,
        start_new_session=start_new_session,
    )

create_shell_process async

create_shell_process(
    command: str,
    stdin: Mode | None = None,
    stdout: Mode | None = None,
    stderr: Mode | None = None,
    limit: int = 10 * 1024 * 1024,
    cwd: str | PathLike[str] | None = None,
    env: dict[str, str] | None = None,
    start_new_session: bool = False,
) -> Process

Small create_subprocess_shell wrapper.

Source code in src/anyenv/processes.py
async def create_shell_process(
    command: str,
    stdin: Mode | None = None,
    stdout: Mode | None = None,
    stderr: Mode | None = None,
    limit: int = 10 * 1024 * 1024,
    cwd: str | os.PathLike[str] | None = None,
    env: dict[str, str] | None = None,
    start_new_session: bool = False,
) -> Process:
    """Small create_subprocess_shell wrapper."""
    return await asyncio.create_subprocess_shell(
        command,
        stdin=MAP[stdin] if stdin else None,
        stdout=MAP[stdout] if stdout else None,
        stderr=MAP[stderr] if stderr else None,
        limit=limit,
        cwd=cwd,
        env=env,
        start_new_session=start_new_session,
    )

hard_kill async

hard_kill(process: Process, *, graceful_timeout: float = 5.0, force_timeout: float = 2.0) -> None

Kill a process with graceful termination followed by force kill if needed.

Parameters:

Name Type Description Default
process Process

The subprocess to terminate

required
graceful_timeout float

Timeout for graceful termination (default: 5.0s)

5.0
force_timeout float

Timeout for force kill (default: 2.0s)

2.0

This function handles cross-platform process termination: - Windows: Uses terminate() then kill() as fallback - Unix: SIGTERM, then SIGKILL after graceful_timeout. When the process leads its own process group (started with start_new_session=True), the signals go to the whole group, so its children die too; they get the whole graceful_timeout even when the leader exits first. Otherwise only the process itself is signalled: its group is the caller's own.

Source code in src/anyenv/processes.py
async def hard_kill(
    process: Process,
    *,
    graceful_timeout: float = 5.0,
    force_timeout: float = 2.0,
) -> None:
    """Kill a process with graceful termination followed by force kill if needed.

    Args:
        process: The subprocess to terminate
        graceful_timeout: Timeout for graceful termination (default: 5.0s)
        force_timeout: Timeout for force kill (default: 2.0s)

    This function handles cross-platform process termination:
    - Windows: Uses terminate() then kill() as fallback
    - Unix: SIGTERM, then SIGKILL after `graceful_timeout`. When the process leads
      its own process group (started with `start_new_session=True`), the signals go
      to the whole group, so its children die too; they get the whole
      `graceful_timeout` even when the leader exits first. Otherwise only the process itself
      is signalled: its group is the caller's own.
    """
    if process.returncode is not None:
        # Process already terminated
        return

    try:
        if sys.platform == "win32":
            # On Windows, terminate the process directly
            process.terminate()
            try:
                # Wait with timeout, then force kill if needed
                await asyncio.wait_for(process.wait(), timeout=graceful_timeout)
            except TimeoutError:
                # Force kill if graceful termination failed
                process.kill()
                await asyncio.wait_for(process.wait(), timeout=force_timeout)
        else:
            await _kill_unix(process, graceful_timeout, force_timeout)

    except TimeoutError, ProcessLookupError, OSError:
        # Process already dead or force kill timed out
        pass