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:
ResiliparseGuardExceptionExecution time limit exceeded.
- exception resiliparse.process_guard.MemoryLimitExceeded
Bases:
ResiliparseGuardExceptionMemory limit exceeded.
- exception resiliparse.process_guard.ResiliparseGuardException
Bases:
BaseExceptionBase exception raised by a Resiliparse process guard.
- class resiliparse.process_guard.MemGuard(native_guard)
Bases:
_GuardDecorator 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:
_GuardDecorator and context manager for guarding the execution time of a task.
Instances are created by
time_guard(). The timeout can be reset by callingprogress()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
MemGuardfor 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_memoryis 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
RuntimeErrorwhen it is entered or called.- Parameters:
max_memory (int) – Maximum allowed RSS or RSS growth in KiB
absolute (bool) – Interpret
max_memoryas an absolute process limitgrace_period (int) – Delay in seconds before the first interrupt
grace_period_ms – First grace period in milliseconds, overriding
grace_periodsecondary_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
SIGKILLinstead ofSIGTERMfor the final attemptcheck_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:
- resiliparse.process_guard.progress(ctx=None)
Report progress to the active
TimeGuardand reset its timeout.If
ctxisNone, 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
TimeGuardcontext, orNonefor 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
ctxisNone, the guard is found usingprogress()’s stack lookup.- Parameters:
it – Iterable to pass through
ctx – Active time-guard context, or
Nonefor 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
TimeGuardfor 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 withTimeGuard.progress()orprogress()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, anExecutionTimeoutis raised after the timeout and again after the grace period if necessary. WithInterruptType.signal, the guard sendsSIGINTfollowed bySIGTERM. The default,InterruptType.exception_then_signal, first raises an exception and then escalates to signals.send_kill=TrueusesSIGKILLfor 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
timeoutgrace_period – Delay in seconds between interrupt attempts, default 15
grace_period_ms – Grace period in milliseconds, overriding
grace_periodinterrupt_type – Exception, signal, or exception-then-signal behavior
send_kill – Use
SIGKILLinstead ofSIGTERMfor the final attemptcheck_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: