nvflare.utils.process_utils module

class ProcessAdapter(process: Popen | None = None, pid: int | None = None)[source]

Bases: object

Adapter to manage a process, whether created via subprocess.Popen or os.posix_spawn.

Parameters:
  • process – The subprocess.Popen object (if created via subprocess)

  • pid – The process ID (if created via posix_spawn, or fallback for process.pid)

poll() int | None[source]

Check if the process has terminated.

Returns:

None if process is still running, otherwise the exit code.

terminate() None[source]

Terminate the process group.

Sends SIGKILL to the entire process group. No need to call process.terminate() separately since SIGKILL already terminates all processes in the group.

wait() None[source]

Wait for the process to terminate.

spawn_process(cmd_args: List[str], env: dict) ProcessAdapter[source]

Launch a process using posix_spawn if available, falling back to subprocess.Popen.

This method attempts to use os.posix_spawn with setsid=True to avoid fork() related issues (such as gRPC deadlocks). If posix_spawn is unavailable or fails, it falls back to subprocess.Popen with preexec_fn=os.setsid.

Parameters:
  • cmd_args – The command arguments as a list of strings.

  • env – The environment variables dictionary.

Returns:

An adapter wrapping the launched process.

Return type:

ProcessAdapter