Skip to content

Timeouts

Utilities for performing local, signal-based timeouts. Implementation from https://github.com/glenfant/stopit under the MIT license.

from prairielearn.timeout_utils import ...

SignalTimeout

Context manager for limiting the execution time of a block.

Parameters:

Name Type Description Default
seconds float | int

duration to run the context manager block

required
swallow_exc bool

False if you want to manage TimeoutExceptionError (or any other) in an outer try ... except structure. True (default) if you just want to check the execution of the block with the state attribute of the context manager.

True

__bool__

__bool__() -> bool

Return whether the context is not TIMED_OUT or INTERRUPTED

__enter__

__enter__() -> Self

Initializes the interrupt and updates state.

__exit__

__exit__(
    exc_type: type[BaseException] | None,
    exc_value: BaseException | None,
    traceback: TracebackType | None,
) -> bool

Manages exceptions & timeout.

__repr__

__repr__() -> str

Debug helper

cancel

cancel() -> None

In case in the block you realize you don't need anymore limitation

setup_interrupt

setup_interrupt() -> None

Setting up the resource that interrupts the block

suppress_interrupt

suppress_interrupt() -> None

Removing the resource that interrupts the block

ThreadingTimeout

Deprecated alias for SignalTimeout.

TimeoutExceptionError

Raised when the block under context management takes longer to complete than the allowed maximum timeout value.