caikit.runtime.work_management.abortable_context ================================================ .. py:module:: caikit.runtime.work_management.abortable_context Attributes ---------- .. autoapisummary:: caikit.runtime.work_management.abortable_context.log Classes ------- .. autoapisummary:: caikit.runtime.work_management.abortable_context.ActionAborter caikit.runtime.work_management.abortable_context.ThreadInterrupter caikit.runtime.work_management.abortable_context.AbortableContext Module Contents --------------- .. py:data:: log .. py:class:: ActionAborter Bases: :py:obj:`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) .. py:method:: must_abort() -> bool :abstractmethod: Indicate whether or not the action must be aborted .. py:method:: set_context(context: AbortableContext) :abstractmethod: Set the abortable context that must be notified to abort work .. py:method:: unset_context() :abstractmethod: Unset any abortable context already held. Do not notify it that work should abort .. py:class:: ThreadInterrupter 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. .. py:attribute:: _SHUTDOWN_SIGNAL :value: -1 .. py:attribute:: _queue .. py:attribute:: _thread :type: Optional[threading.Thread] :value: None .. py:attribute:: _context_thread_map :type: Dict[uuid.UUID, int] .. py:attribute:: _start_stop_lock .. py:method:: start() Start the watch loop that will abort any registered contexts passed to .kill() .. py:method:: stop() Stop the watch loop .. py:method:: register(context_id: uuid, thread: int) -> None .. py:method:: unregister(context_id: uuid) -> None .. py:method:: kill(context_id: uuid) -> None .. py:method:: _watch_loop() .. py:method:: _kill_thread(context_id: uuid.UUID) -> bool .. py:class:: AbortableContext(aborter: ActionAborter, interrupter: ThreadInterrupter) 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. .. py:attribute:: aborter .. py:attribute:: interrupter .. py:attribute:: id .. py:method:: __enter__() .. py:method:: __exit__(exc_type, exc_val, exc_tb) .. py:method:: abort() Called by the aborter when this context needs to be aborted