Skip to content

CallbackExecutor

veltix.handler.callback_executor.CallbackExecutor

Executes callbacks in a thread pool to avoid blocking the recv loop.

Instead of calling on_recv() directly in the receive thread, the RequestHandler submits the call to this executor. This ensures that slow or blocking callbacks (heavy computation, sleep, I/O) never delay message reception.

Usage::

executor = CallbackExecutor(max_workers=4)
executor.submit(on_recv, client, response)
executor.shutdown()

__init__

__init__(max_workers: int = 4) -> None

Initialize the callback executor.

Parameters:

Name Type Description Default
max_workers int

Maximum number of concurrent callback threads (default: 4). Increase for high-concurrency workloads with slow callbacks.

4

submit

submit(func: Callable, *args: Any) -> None

Submit a callback for asynchronous execution.

Returns immediately — the callback runs in a worker thread. Exceptions raised inside the callback are caught and logged, so they never propagate back to the recv loop.

Parameters:

Name Type Description Default
func Callable

Callback function to execute

required
*args Any

Arguments to pass to the callback

()

shutdown

shutdown() -> None

Shutdown the executor gracefully.

Waits for all running callbacks to complete before returning. Should be called when the server/client is shutting down.