Resiliparse Process Guards

Resiliparse Process Guard API documentation.

class resiliparse.process_guard.InterruptType

Method used to interrupt a task after a guard limit is exceeded.

Resiliparse context guard interrupt type.

exception = InterruptType.exception

Send only exceptions

signal = InterruptType.signal

Send only signals

exception_then_signal = InterruptType.exception_then_signal

Send an exception first and follow up with signals

exception resiliparse.process_guard.ExecutionTimeout

Bases: ResiliparseGuardException

Execution time limit exceeded.

exception resiliparse.process_guard.MemoryLimitExceeded

Bases: ResiliparseGuardException

Memory limit exceeded.

exception resiliparse.process_guard.ResiliparseGuardException

Bases: BaseException

Base exception raised by a Resiliparse process guard.

class resiliparse.process_guard.MemGuard(native_guard)

Bases: _Guard

Decorator and context manager for limiting Linux process memory usage.

Instances are created by mem_guard().

Parameters:

native_guard (Any)

class resiliparse.process_guard.TimeGuard(native_guard)

Bases: _Guard

Decorator and context manager for guarding the execution time of a task.

Instances are created by time_guard(). The timeout can be reset by calling progress() from the guarded code.

Parameters:

native_guard (Any)

progress()

Report progress and restart the guard timeout countdown.

Return type:

None

resiliparse.process_guard.mem_guard(max_memory, absolute=True, grace_period=0, grace_period_ms=None, secondary_grace_period=5, secondary_grace_period_ms=None, interrupt_type=InterruptType.exception_then_signal, send_kill=False, check_interval=500)

Create a MemGuard for enforcing a process-memory limit on Linux.

If process RSS exceeds max_memory, a grace timer starts. The timer resets if memory usage falls below the limit. If the limit remains exceeded, the guard interrupts the task and follows the selected escalation procedure.

With absolute=True, max_memory is the total RSS limit in KiB. Otherwise it is the allowed RSS growth above the value measured when the guard starts. The first grace period defaults to zero; later escalation attempts are separated by the secondary grace period, which defaults to five seconds.

Warning

Memory guards are supported only on Linux. Using the guard on another platform raises RuntimeError when it is entered or called.

Parameters:
  • max_memory (int) – Maximum allowed RSS or RSS growth in KiB

  • absolute (bool) – Interpret max_memory as an absolute process limit

  • grace_period (int) – Delay in seconds before the first interrupt

  • grace_period_ms – First grace period in milliseconds, overriding grace_period

  • secondary_grace_period (int) – Delay in seconds between later escalation attempts

  • secondary_grace_period_ms – Secondary grace period in milliseconds

  • interrupt_type – Exception, signal, or exception-then-signal behavior

  • send_kill (bool) – Use SIGKILL instead of SIGTERM for the final attempt

  • check_interval (int) – Watchdog polling interval in milliseconds, default 500

Returns:

A decorator and context manager for memory limits

Raises:
  • OverflowError – If a memory limit or duration is negative or out of range

  • RuntimeError – When entering the guard outside Linux or inside another memory guard

  • MemoryLimitExceeded – In the guarded task if the memory limit is exceeded and exceptions are enabled

Return type:

MemGuard

resiliparse.process_guard.progress(ctx=None)

Report progress to the active TimeGuard and reset its timeout.

If ctx is None, search the Python call stack for a guard context in the global namespace or a decorated function carrying _guard_self. If the context is local or cannot be found, pass it explicitly.

Parameters:

ctx – Active TimeGuard context, or None for stack lookup

Raises:

RuntimeError – If no active time guard can be found

Return type:

None

resiliparse.process_guard.progress_loop(it, ctx=None)

Wrap an iterable and report progress to an active time guard after each item.

This pass-through helper is useful when every iteration should reset the timeout. The guard is advanced when iteration resumes after yielding an item. If ctx is None, the guard is found using progress()’s stack lookup.

Parameters:
  • it – Iterable to pass through

  • ctx – Active time-guard context, or None for automatic lookup

Returns:

Iterator yielding the original items

Raises:
  • TypeError – During iteration if the input is not iterable

  • RuntimeError – During iteration if no active time guard can be found

Return type:

Iterator[Any]

resiliparse.process_guard.time_guard(timeout=60, timeout_ms=None, grace_period=15, grace_period_ms=None, interrupt_type=InterruptType.exception_then_signal, send_kill=False, check_interval=500)

Create a TimeGuard for guarding the execution time of a running task.

The guard can be used as a decorator or context manager. If the guarded task exceeds timeout, the configured interrupt is sent. Reporting progress with TimeGuard.progress() or progress() resets the timer.

There are two interrupt mechanisms: throwing an asynchronous exception and sending a UNIX signal. Exceptions are gentle but may not interrupt blocking native calls or system calls. Signals are more reliable, but only the interpreter’s main thread can receive and handle them.

With interrupt_type=InterruptType.exception, an ExecutionTimeout is raised after the timeout and again after the grace period if necessary. With InterruptType.signal, the guard sends SIGINT followed by SIGTERM. The default, InterruptType.exception_then_signal, first raises an exception and then escalates to signals. send_kill=True uses SIGKILL for the final signal attempt; this terminates the whole process.

Parameters:
  • timeout – Maximum execution time in seconds, default 60

  • timeout_ms – Maximum execution time in milliseconds, overriding timeout

  • grace_period – Delay in seconds between interrupt attempts, default 15

  • grace_period_ms – Grace period in milliseconds, overriding grace_period

  • interrupt_type – Exception, signal, or exception-then-signal behavior

  • send_kill – Use SIGKILL instead of SIGTERM for the final attempt

  • check_interval – Watchdog polling interval in milliseconds, default 500

Returns:

A decorator and context manager for execution-time limits

Raises:
  • ValueError – If both timeout and timeout_ms are None

  • OverflowError – If a duration is negative or out of range

  • RuntimeError – When entering a nested time guard

  • ExecutionTimeout – In the guarded task if its timeout is exceeded and exceptions are enabled

Return type:

TimeGuard