SoloThreadedTask

Methods

qconcurrency.threading_.SoloThreadedTask.__init__(…)
param callback:A function, method, or class that you would like to run in
qconcurrency.threading_.SoloThreadedTask.is_active() Returns True if this SoloThreadedTask is overseeing an active thread.
qconcurrency.threading_.SoloThreadedTask.start([…]) Creates/starts a new ThreadedTask, and cancels all other pending/running threads started by this SoloThreadedTask instance.
qconcurrency.threading_.SoloThreadedTask.stop([…]) Emits request_abort signal on all threads up-to (but not including) the target until_threadId.

Documentation

class qconcurrency.threading_.SoloThreadedTask(callback, signals=None, connections=None, mutex_expiry=5000)[source]

Bases: object

ThreadedTask that cancels all of it’s running/pending threads (started by this SoloThreadedTask) whenever a new thread is requested (and all must exit before the latest requested task is allowed to start ).

This is designed for methods that load or filter the contents of a widget.

Warning

If slot connected to one of this task’s signals does not update UI until this thread exits, add the following line to the end of your slot.

QtCore.QCoreApplication.instance().processEvents()

Example

class MyList( QtWidgets.QListWidget ):
    def __init__(self):
        self._thread_loading = SoloThreadedTask(
            callback    = self._find_list_items,
            signals     = {'add_item': str},
            connections = {'add_item': [self.addItem] },
        )

    def load(self):
        #
        # whenever `self.load` is called
        # the last load will be cancelled,
        # after which a new load process will start
        #
        self._thread_loading.start()

    def _find_list_items(self, signalmgr=None ):
        for i in range(100):
            signalmgr.handle_if_abort()   # check for a request-abort, and exit early
            time.sleep(1)
            signalmgr.add_item.emit( i )  # add an item to the list
__init__(callback, signals=None, connections=None, mutex_expiry=5000)[source]
Parameters:
  • callback (callable) – A function, method, or class that you would like to run in a separate thread.
  • signals (dict, optional) –

    Dictionary of signal-names, and the datatypes they will emit. Signals defined here will override any default signals.

    {
        # signal-name #  # datatype #   # equivalent-to #
    
        ‘add_item’:       (int,str),    #: QtCore.Signal(int,str)
        ‘add_progress’:   int,          #: QtCore.Signal(int)
        ‘returned’:       None,         #: QtCore.Signal()
    }
    
  • connections (dict, optional) –

    Dictionary of signal-names, and a python-callable, or list of python-callables to connect to the signal.

    {
        ‘add_item’:     [ printargs, mylist.addItem ],
        ‘add_progress’: progbar.add_progress,
        …
    }
    
  • *args/**kwds

    Any additional arguments/keyword-arguments are passed to the callback in run()

is_active()[source]

Returns True if this SoloThreadedTask is overseeing an active thread.

start(expiryTimeout=-1, threadpool=None, wait=False, _connections=None, *args, **kwds)[source]

Creates/starts a new ThreadedTask, and cancels all other pending/running threads started by this SoloThreadedTask instance.

Parameters:
  • expiryTimeout (int, optional) – Thread that unused for N milliseconds are considered expired and will exit. By default, no exipiryTimeout is set (-1).
  • threadpool (QtCore.QThreadPool, optional) – By default, this ThreadedTask will be queued in the QCoreApplication’s global threadpool. If you would prefer to assign another, you may specify it here.
  • _connections (dict, optional) – Entirely replaces the connections defined in __init__ . Only use this if you are absolutely certain you know what you are doing.
  • wait (int, bool, optional) – (ex: True, False, 1.5 ) Similar to threading.Thread.join() this allows you to wait for the thread to complete, or wait N seconds for it to complete. If the timeout expires, the exception TimedOut is raised.
  • *args/**kwds

    Any additional arguments/keyword-arguments will be passed to the callback defined in __init__() when it is run from it’s separate thread.

stop(until_threadId=None, wait=None)[source]

Emits request_abort signal on all threads up-to (but not including) the target until_threadId. If until_threadId is not provided, all pending threads are killed.

(threadIds will be automatically removed from attr _active_threads as they return, or raise unhandled exceptions).

Parameters:
  • until_threadId – Requests abort on all running threads until (and not including) the thread with a threadId matching until_threadId.
  • wait (numbers.Number, optional) – (ex: None, -1, 100) Optionally, wait N seconds for job to complete after requesting abort. If negative number, waits indefinitely. Waiting in the main UI thread will result in a deadlock.
Raises
  • TimedOut if user set a wait time.