pycroglia.core.compute package
Subpackages
Submodules
- class pycroglia.core.compute.backend.Backend(*args, **kwargs)[source]
Bases:
ProtocolProtocol for computation backends.
Defines a common interface for running tasks, regardless of whether they are executed via PyQt, multiprocessing, or another concurrency model.
- submit(task, on_result, on_error=None, on_finish=None)[source]
Submit a task to the backend.
- Parameters:
task (Task) – Task to be executed.
on_result (Callable) – Callback for successful results.
on_error (Callable | None) – Optional callback for errors.
on_finish (Callable | None) – Optional callback when task finishes.
- Return type:
None
- class pycroglia.core.compute.backend.Task(*args, **kwargs)[source]
Bases:
ProtocolProtocol for executable tasks.
Any class implementing this protocol must define a run method that returns a dictionary with computation results.
- class pycroglia.core.compute.computable.Computable[source]
Bases:
ABCAbstract base class for all computable objects.
Classes inheriting from Computable must implement the compute method. The compute method executes a specific computation and returns its results as a dictionary, allowing flexible storage of heterogeneous values.
- class pycroglia.core.compute.mp_pool.CancelFlag[source]
Bases:
objectLightweight cooperative cancellation flag for multiprocessing tasks.
This class provides a simple shared mechanism to signal cancellation across multiple worker processes using a multiprocessing.Manager.Value. Each Computable should periodically check the flag to terminate gracefully.
Example
```python flag = CancelFlag() if flag.is_set():
return # Stop early
- _cancelled
Shared boolean indicating cancellation state.
- Type:
mp.Value
- class pycroglia.core.compute.mp_pool.MPPool(processes=None)[source]
Bases:
objectMultiprocessing pool manager for executing Computable tasks.
Provides submission, execution, and completion tracking for multiple concurrent tasks.
- Parameters:
processes (int | None)
- all_finished
Optional callback invoked when all submitted tasks have completed.
- Type:
Callable[[], None] | None
- cancel()[source]
Request cooperative cancellation for all running tasks.
Sets the shared cancellation flag. All tasks that periodically check it will terminate gracefully. Pending tasks are ignored.
- Return type:
None
- class pycroglia.core.compute.mp_pool.MPTask(computable, cancel_flag)[source]
Bases:
objectA multiprocessing-compatible task that executes a Computable.
- Parameters:
computable (Computable)
cancel_flag (CancelFlag)
- computable
The computation object to run.
- Type:
- cancel_flag
Shared cancellation flag.
- Type:
- class pycroglia.core.compute.pool.Pool(backend)[source]
Bases:
objectUnified Pool that delegates to a specific backend.
This class acts as a façade: it exposes a single API (submit, run, join) while delegating execution to the provided backend (e.g. QPool, MPPool).
- Parameters:
backend (Backend)
- class pycroglia.core.compute.qt_pool.CancelFlag[source]
Bases:
objectLightweight cooperative cancellation flag for task management.
This class provides a simple mechanism for signalling cooperative cancellation between a task backend (e.g., QThreadPool) and the tasks it executes.
- class pycroglia.core.compute.qt_pool.QPool[source]
Bases:
QObjectThread pool manager for executing Computable tasks.
Provides submission, execution, and completion tracking for multiple concurrent tasks.
- all_finished
Emitted when all submitted tasks have completed.
- Type:
pyqtSignal
- all_finished
str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL
types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.
- Type:
pyqtSignal(*types, name
- join()[source]
Block until all tasks are finished.
Warning
This method blocks the main (GUI) thread if called inside a running Qt event loop. Prefer connecting to all_finished for a non-blocking alternative.
- Return type:
None
- class pycroglia.core.compute.qt_pool.QTask(computable, flag)[source]
Bases:
QRunnableA runnable task that executes a Computable object in a thread.
- Parameters:
computable (Computable)
flag (CancelFlag)
- computable
The computation object to run.
- Type:
- signals
Signal manager for result, error, and finished.
- Type:
TaskSignals
- class pycroglia.core.compute.qt_pool.QTaskSignal[source]
Bases:
QObjectSignals available from a running Task.
- result
Emitted with the computation result as a dict.
- Type:
pyqtSignal
- error
Emitted with (task_id, exception) if computation fails.
- Type:
pyqtSignal
- finished
Emitted with task_id when the task completes.
- Type:
pyqtSignal
- error
str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL
types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.
- Type:
pyqtSignal(*types, name
- finished
str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL
types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.
- Type:
pyqtSignal(*types, name
- result
str = …, revision: int = …, arguments: Sequence = …) -> PYQT_SIGNAL
types is normally a sequence of individual types. Each type is either a type object or a string that is the name of a C++ type. Alternatively each type could itself be a sequence of types each describing a different overloaded signal. name is the optional C++ name of the signal. If it is not specified then the name of the class attribute that is bound to the signal is used. revision is the optional revision of the signal that is exported to QML. If it is not specified then 0 is used. arguments is the optional sequence of the names of the signal’s arguments.
- Type:
pyqtSignal(*types, name