caikit.runtime.work_management.abortable_context

Attributes

log

Classes

ActionAborter

Simple interface to wrap up a notification that an action must abort.

ThreadInterrupter

This class implements a listener which will observe all ongoing work in AbortableContexts

AbortableContext

Context manager for running work inside a context where it's safe to abort.

Module Contents

caikit.runtime.work_management.abortable_context.log[source]
class caikit.runtime.work_management.abortable_context.ActionAborter[source]

Bases: abc.ABC

Simple interface to wrap up a notification that an action must abort.

Children of this class can bind to any notification tool (e.g. grpc context)

abstract must_abort() bool[source]

Indicate whether or not the action must be aborted

abstract set_context(context: AbortableContext)[source]

Set the abortable context that must be notified to abort work

abstract unset_context()[source]

Unset any abortable context already held. Do not notify it that work should abort

class caikit.runtime.work_management.abortable_context.ThreadInterrupter[source]

This class implements a listener which will observe all ongoing work in AbortableContexts and raise exceptions in the working threads if they need to be aborted.

The implementation spawns a single extra thread to wait on any contexts to abort, and interrupt the thread that the context is running in. This keeps the total number of running threads much smaller than using a new thread to monitor each AbortableContext.

_SHUTDOWN_SIGNAL = -1
_queue
_thread: threading.Thread | None = None
_context_thread_map: Dict[uuid.UUID, int]
_start_stop_lock
start()[source]

Start the watch loop that will abort any registered contexts passed to .kill()

stop()[source]

Stop the watch loop

register(context_id: uuid, thread: int) None[source]
unregister(context_id: uuid) None[source]
kill(context_id: uuid) None[source]
_watch_loop()[source]
_kill_thread(context_id: uuid.UUID) bool[source]
class caikit.runtime.work_management.abortable_context.AbortableContext(aborter: ActionAborter, interrupter: ThreadInterrupter)[source]

Context manager for running work inside a context where it’s safe to abort.

This is a class instead of a @contextmanager function because __exit__ needs to happen on exception.

aborter
interrupter
id
__enter__()[source]
__exit__(exc_type, exc_val, exc_tb)[source]
abort()[source]

Called by the aborter when this context needs to be aborted