QRunnable with signals that wraps a simple long-running function.
QRunnable with signals that wraps a long-running generator.
Base class for creating a Worker that can run in another thread.
Return the number of active threads in the global ThreadPool.
- Return type
create_worker(func, *args, _start_thread=None, _connect=None, _progress=None, _worker_class=None, _ignore_errors=False, **kwargs)¶
Convenience function to start a function in another thread.
By default, uses
Worker, but a custom
WorkerBasesubclass may be provided. If so, it must be a subclass of
Worker, which defines a standard set of signals and a run method.
func (Callable) – The function to call in another thread.
_start_thread (bool, optional) – Whether to immediaetly start the thread. If False, the returned worker must be manually started with
worker.start(). by default it will be
_connect (Dict[str, Union[Callable, Sequence]], optional) – A mapping of
callableor list of
callable: callback functions to connect to the various signals offered by the worker class. by default None
_progress (Union[bool, Dict[str, Union[int, bool, str]]], optional) – Can be True, to provide indeterminate progress bar, or dictionary. If dict, requires mapping of ‘total’ to number of expected yields. If total is not provided, progress bar will be indeterminate. Will connect progress bar update to yields and display this progress in the viewer. Can also take a mapping of ‘desc’ to the progress bar description. Progress bar will become indeterminate when number of yields exceeds ‘total’. By default None.
_worker_class (Type[WorkerBase], optional) – The :class`WorkerBase` to instantiate, by default
FunctionWorkerwill be used if
funcis a regular function, and
GeneratorWorkerwill be used if it is a generator.
_ignore_errors (bool, optional) – If
False(the default), errors raised in the other thread will be reraised in the main thread (makes debugging significantly easier).
*args – will be passed to
**kwargs – will be passed to
worker – An instantiated worker. If
False, the worker will have a .start() method that can be used to start the thread.
- Return type
def long_function(duration): import time time.sleep(duration) worker = create_worker(long_function, 10)
Set the maximum number of threads used by the thread pool.
Note: The thread pool will always use at least 1 thread, even if maxThreadCount limit is zero or negative.