mechanics is a module written to allow the execution, tracking, management and destruction of parallel processes through a simple API in PySide6 GUI programs.
In practice, this means the creation of one or more simultaneous processes passing defined input arguments, receiving information from the processes regarding their execution progress, automatic termination if an arbitrary timeout is reached, forced, instantaneous termination of a process at any time, 'clean' termination of a process at any time, and total termination of processes when the main program finishes running.
In mechanics, processes are run through the Python multiprocessing module, so they can dodge the limits imposed by GIL and can be terminated at any time. Besides, they are encapsulated by a custom object that is accessed through a _thread, effectively allowing it to communicate with the code that created it through a Signal, potentially being able to indicate its progress during its execution. Processes do not block the GUI.
A process can have a timeout assigned on its creation. In this case, mechanics will automatically terminate it once the timeout is reached (if it is still running). A running process can also be arbitrarily terminated at any time. mechanics can also ask the process to stop, and provides means to allow the process to receive the request for termination and, effectively, terminate in a timely, 'clean' way. mechanics also handles the termination of processes that may be running when the main GUI program is terminated.
Click here
to download the code. It is a compressed file containing two
python files: mechanics.py (the module itself), and
test_mechanics.py (a small PySide6 demo that shows the
capabilities of the module by calculating π using the Bailey–Borwein–Plouffe
formula). There is more information in the code itself and
its comments — don't forget to take a closer look to them.
Tested on PySide
6.5.2 on Python 3.10.12 on
Mint (Xfce) and on PySide
6.6.1 on Python
3.10.2 on Windows 11.
The files mentioned here are distributed under the BSD-3-Clause
License.
Changes the maximum allowed number of concurrent processes.
Changes the polling interval when checking processes (in seconds).
Changes logging verbosity. log_level options are: "debug", "info", "warning", "error", "critical".
Sets the default finishing method: can be either terminate or kill.
Returns the number of running processes plus pooled processes.
Returns a ProcX object from a tsid (descriptor), or None if the tsid is not on the current processes list.
Emits the signal supplied with this tsid with tsid itself, command and value as args.
Starts a new callable process. obj is the function or method to be run as the process.
desc | (string) an arbitrary small description of the
process. |
timeout |
(int) period of time (in seconds) that will be
allowed to elapse before this process is stopped. |
signal |
(PySide6 Signal) the custom signal that should be bound to the application to fetch the process calls |
args | (tuple) arguments to the process |
Returns, if the creation was
successful, a string descriptor (tsid)
containing the description and the creation timestamp. Returns
None if the process already exists.
Asks nicely to stop a process by its string descriptor (tsid). If implemented on the process itself (by watching ShouldKeepGoing), it may end gracefully by calling Comm("Finish").
Given a process string descriptor (tsid), stops (terminates or kills) the process, according to the method passed. If no stopping method is passed, it will use the default finish method currently set.
Stops (terminates or kills) all running processes according to the method passed. If no stopping method is passed, it will use the default finish method currently set.
This method will stop all running processes and the gear thread. It should be called before exiting the QApplication (e.g, upon the closeEvent method execution). If no stopping method is passed, it will use the default finish method currently set.