helperFunctions.process module
- class helperFunctions.process.ExceptionSafeProcess(*args, reraise=True, **kwargs)
Bases:
Process
ExceptionSafeProcess is a subtype of
multiprocessing.Process
with added exception handling. Opposed to what the name may suggest, the class does not make the process impervious to exceptions. Instead, it retrieves the exception of the subprocess and re-raises it. This allows reconstructing what happened in the subprocess.The parameters for creating an instance of the class are the same as for
multiprocessing.Process
(see Python docs).- Parameters:
reraise (bool) –
- property exception: tuple[Exception, str] | None
The exception that occurred in the process during execution and the respective stack trace. Is
None
if no exception occurred or the process was no yet executed.- Returns:
A tuple containing the exception and the stack trace or
None
.
- run()
Starts the execution of the process. Any exception happening in the process will be reraised and may be retrieved by accessing
ExceptionSafeProcess.exception
.
- helperFunctions.process.check_worker_exceptions(process_list, worker_label, worker_function=None)
Iterate over the process_list and check if exceptions occurred. In case of an exception, the process and its children will be terminated. If
throw_exceptions
in the FACT configuration is set to false, the worker may be restarted by passing a function (if the value is not set, the worker will not be restarted). In this case, the function will always returnFalse
. Ifthrow_exceptions
is set to true and an exception occurs, the worker will not be restarted and the return value isTrue
.- Parameters:
process_list (list[ExceptionSafeProcess]) – A list of worker processes.
worker_label (str) – A label used for logging (e.g. Analysis or Unpacking).
worker_function (Callable | None) – A function used for restarting the worker (optional).
- Returns:
True
if an exception occurred in any process andthrow_exceptions
in the FACT configuration is set to true andFalse
otherwise.- Return type:
bool
- helperFunctions.process.complete_shutdown(message=None)
Shutdown all FACT processes (of the currently running component) by sending a signal to the process group.
- Parameters:
message (str | None) – Optional message to be displayed before the shutdown.
- Return type:
None
- helperFunctions.process.new_worker_was_started(new_process, old_process)
Check if a worker process was restarted by comparing old and new process.
- Parameters:
new_process (ExceptionSafeProcess) – The new process.
old_process (ExceptionSafeProcess) – The old process.
- Returns:
True
if the processes match andFalse
otherwise.- Return type:
bool
- helperFunctions.process.start_single_worker(process_index, label, function)
Starts a new worker process executing
function
and returns it. Used for unpacking and analysis workers in the FACT backend.- Parameters:
process_index (int) – The index of the process in the process list of the scheduler.
label (str) – A label used for logging (e.g. Analysis or Unpacking).
function (Callable) – The function, that gets executed by the worker process.
- Returns:
The running process.
- Return type:
- helperFunctions.process.stop_process(process, timeout=10.0)
Try to stop a single process gracefully. If it does not stop until timeout is reached, kill it.
- Parameters:
process (Process) –
timeout (float) –
- helperFunctions.process.stop_processes(processes, timeout=10.0)
Try to stop processes gracefully in parallel. If a process does not stop until timeout is reached, kill it.
- Parameters:
processes (list[Process]) – The list of processes that should be stopped.
timeout (float) – Timeout for joining the process in seconds.
- helperFunctions.process.terminate_process_and_children(process)
Terminate a process and all of its child processes.
- Parameters:
process (Process) – The process to be terminated.
- Return type:
None