Skip to content

ThreadPool

Base classes

Name Children Inherits
ObjectMixin
prettyqt.core.object
QThreadPool
PySide6.QtCore
QThreadPool(self, /, parent: PySide6.QtCore.QObject | None \= None, *, expiryTimeout: int | None \= None, maxThreadCount: int | None \= None, activeThreadCount: int | None \= None, stackSize: int | None \= None, threadPriority: PySide6.QtCore.QThread.Priority | None \= None) -> None

⋔ Inheritance diagram

graph TD
  3901574290["core.ThreadPool"]
  3227531497["core.ObjectMixin"]
  2751680898["builtins.object"]
  1984540435["QtCore.QThreadPool"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  3227531497 --> 3901574290
  2751680898 --> 3227531497
  1984540435 --> 3901574290
  3984060476 --> 1984540435
  37984305 --> 3984060476
  2751680898 --> 37984305

🛈 DocStrings

Bases: ObjectMixin, QThreadPool

Manages a collection of QThreads.

Note: signals only work correctly when exclusively using start_worker method.

Source code in prettyqt/core/threadpool.py
class ThreadPool(core.ObjectMixin, core.QThreadPool):
    """Manages a collection of QThreads.

    Note: signals only work correctly when exclusively using start_worker method.
    """

    __instance: Self | None = None  # a global instance

    job_num_updated = core.Signal(int)
    error_occured = core.Signal(Exception)
    busy_state_changed = core.Signal(bool)

    def __contains__(self, other: core.QThread):
        return self.contains(other)

    def get_thread_priority(self) -> core.thread.PriorityStr:
        return core.thread.PRIORITY.inverse[self.threadPriority()]

    def set_thread_priority(self, priority: core.thread.PriorityStr):
        prio = core.thread.PRIORITY[priority]
        self.setThreadPriority(prio)

    @classmethod
    def instance(cls) -> Self:
        """Return global ThreadPool singleton. (globalInstance always returns Qt type)."""
        if cls.__instance is None:
            cls.__instance = cls()
        return cls.__instance

    def start_worker(
        self,
        fn_or_worker: Callable | Worker,
        args: tuple | None = None,
        kwargs: dict | None = None,
        priority: int = 0,
        result_fn: Callable | None = None,
        finished_fn: Callable | None = None,
        progress_fn: Callable | None = None,
        error_fn: Callable | None = None,
    ):
        if isinstance(fn_or_worker, Callable):
            if args is None:
                args = ()
            if kwargs is None:
                kwargs = {}
            runnable = Worker(fn_or_worker, *args, **kwargs)
        else:
            runnable = fn_or_worker
        runnable.signals.finished.connect(self._on_job_ended)
        runnable.signals.error.connect(self._on_job_ended)
        runnable.signals.error.connect(self._on_exception)
        if result_fn:
            runnable.signals.result.connect(result_fn)
        if finished_fn:
            runnable.signals.finished.connect(finished_fn)
        if progress_fn:
            runnable.signals.progress.connect(progress_fn)
        if error_fn:
            runnable.signals.error.connect(error_fn)
        thread_count = self.activeThreadCount()
        if thread_count == 0:
            self.busy_state_changed.emit(True)
        self.job_num_updated.emit(thread_count + 1)  # + 1 because we didnt start yet.
        super().start(runnable, priority)

    def _on_job_ended(self):
        thread_count = self.activeThreadCount()
        if thread_count == 1:  # this is the last job
            self.busy_state_changed.emit(False)
        self.job_num_updated.emit(thread_count - 1)  # -1 because we didnt really end yet.

    def _on_exception(self, exception):
        self.error_occured.emit(exception)

instance() -> Self classmethod

Return global ThreadPool singleton. (globalInstance always returns Qt type).

Source code in prettyqt/core/threadpool.py
@classmethod
def instance(cls) -> Self:
    """Return global ThreadPool singleton. (globalInstance always returns Qt type)."""
    if cls.__instance is None:
        cls.__instance = cls()
    return cls.__instance

:fa-brands-github: Show source on GitHub