mamut_routing_lib.solvers.external.process¶
Run one solver process: its own process group, a hard timeout, cooperative cancellation.
- The solver starts in a new session (POSIX) or process group (Windows), so a
stop request or a timeout kills the whole tree -- a Java launcher, a shell
wrapper, their children -- not just the immediate child. SIGTERM first, then
SIGKILL after
term_grace_s. - Output goes to a log file, never a pipe (a chatty solver would block on a
full pipe). The runner tails the log every
poll_sseconds and parses the descriptor'sprogress_pattern(named groupscostand optionallyiterations/penalty; a line with a non-zero penalty is not an incumbent). - Every poll calls
monitor.observe(elapsed time, incumbent in solver units), which is also where a stop request is noticed. - Live process groups are registered; :func:
terminate_all(also run at interpreter exit) kills them, so Ctrl-C in a CLI or a GUI shutdown leaves no orphan solver behind -- a solver in its own session does not receive the terminal's SIGINT. - A parent that is killed runs none of this: SIGKILL, an unhandled SIGTERM
(
timeout 60 mamut-routing solve, a systemd stop) or a crash end it without the exit hook, and the hard timeout lives in its poll loop. Each registered solver therefore has a guard (POSIX): a small process that holds the read end of a pipe whose only writer is this process. When this process dies, for whatever reason, the guard reads end of file and kills the solver's process group. A launcher waits for the guard to be ready before it executes solver code. A run that ends normally writes one byte first, and the guard exits without killing. A process forked from this one withoutexecwhile a solve runs inherits the pipe, and the guard then waits for it too. - :func:
interrupt_on_terminationturns SIGTERM and SIGHUP into the interrupt that Ctrl-C raises, for a command line that reports what it stopped.
ProcessOutcome
dataclass
¶
ProcessOutcome(
returncode: int | None,
termination: Literal[
"exited", "timeout", "cancelled", "error"
],
wall_time: float,
log_path: Path,
log_tail: str,
best_cost: float | None = None,
iterations: int | None = None,
error: str | None = None,
)
TerminationSignal
¶
Bases: KeyboardInterrupt
SIGTERM or SIGHUP, raised as the interrupt of Ctrl-C by :func:interrupt_on_termination.
Source code in MAMUT-routing-lib/src/mamut_routing_lib/solvers/external/process.py
live_process_count
¶
kill_process_tree
¶
Terminate the process and its group: SIGTERM, then SIGKILL after term_grace_s.
Source code in MAMUT-routing-lib/src/mamut_routing_lib/solvers/external/process.py
terminate_all
¶
Kill every live solver process group started by this interpreter; returns how many.
Source code in MAMUT-routing-lib/src/mamut_routing_lib/solvers/external/process.py
interrupt_on_termination
¶
Inside the block, SIGTERM and SIGHUP raise :class:TerminationSignal in the main thread.
It is a KeyboardInterrupt, so a command line that stops its solvers and reports on Ctrl-C
does the same for timeout 60 <command>, a systemd stop or a closed terminal, which
otherwise end the process without a word. The former handlers are restored on exit. Python
installs signal handlers from the main thread only: anywhere else this does nothing.
Source code in MAMUT-routing-lib/src/mamut_routing_lib/solvers/external/process.py
run_process
¶
run_process(
argv: Sequence[str],
*,
cwd: Path,
log_path: Path,
time_limit_s: float,
grace_s: float = 30.0,
term_grace_s: float = 2.0,
poll_s: float = 0.25,
env: Mapping[str, str] | None = None,
monitor: SolveMonitor | None = None,
progress_pattern: str | None = None,
) -> ProcessOutcome
Run argv until it exits, its budget plus grace_s runs out, or monitor asks to stop.
Source code in MAMUT-routing-lib/src/mamut_routing_lib/solvers/external/process.py
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 | |