Skip to main content

apply_timeout

Executes a target function within a specified time limit, returning the result of the target or invoking a timeout callback if the duration is exceeded.

def apply_timeout(
target: callable,
args: tuple,
kwargs: dict,
callback: callable,
accept_callback: callable,
getpid: callable,
timeout: float,
timeout_callback: callable,
Timeout: Exception,
apply_target: callable
) - > Any

Executes a target function within a specified time limit and handles potential timeout exceptions via a callback.

Parameters

NameTypeDescription
targetcallableThe function or task to be executed.
argstuplePositional arguments to pass to the target function.
kwargsdictKeyword arguments to pass to the target function.
callbackcallableFunction to be invoked upon successful completion of the target.
accept_callbackcallableFunction to be invoked when the task is accepted for execution.
getpidcallableA function that returns the process ID associated with the execution.
timeoutfloatThe maximum duration in seconds allowed for the target function to run.
timeout_callbackcallableFunction invoked if the execution exceeds the timeout limit; it receives the success status and timeout value.
TimeoutExceptionThe exception class used to manage and catch timeout events.
apply_targetcallableThe underlying execution wrapper used to invoke the target with the provided context.

Returns

TypeDescription
AnyThe result of the target function if successful, or the result of the timeout_callback if the time limit is exceeded.