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


napari.qt.threading.create_worker(func, *args, _start_thread=None, _connect=None, _progress=None, _worker_class=None, _ignore_errors=False, **kwargs)[source]

Convenience function to start a function in another thread.

By default, uses Worker, but a custom WorkerBase subclass 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 False if the _connect argument is None, otherwise True.

  • _connect (Dict[str, Union[Callable, Sequence]], optional) – A mapping of "signal_name" -> callable or 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 FunctionWorker will be used if func is a regular function, and GeneratorWorker will 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 func

  • **kwargs – will be passed to func


worker – An instantiated worker. If _start_thread was False, the worker will have a .start() method that can be used to start the thread.

Return type


  • TypeError – If a worker_class is provided that is not a subclass of WorkerBase.

  • TypeError – If _connect is provided and is not a dict of {str: callable}

  • TypeError – If _progress is provided and function is not a generator


def long_function(duration):
    import time

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.