napari.qt.threading.GeneratorWorker¶
-
class
napari.qt.threading.
GeneratorWorker
(func, *args, SignalsClass=<class 'napari._qt.qthreading.GeneratorWorkerSignals'>, **kwargs)[source]¶ Bases:
napari._qt.qthreading.WorkerBase
QRunnable with signals that wraps a long-running generator.
Provides a convenient way to run a generator function in another thread, while allowing 2-way communication between threads, using plain-python generator syntax in the original function.
- Parameters
func (callable) – The function being run in another thread. May be a generator function.
SignalsClass (type, optional) – A QObject subclass that contains signals, by default GeneratorWorkerSignals
*args – Will be passed to func on instantiation
**kwargs – Will be passed to func on instantiation
Methods
Attributes
-
create
(Callable[[], None]) → QRunnable¶
-
quit
()¶ Send a request to abort the worker.
Note
It is entirely up to subclasses to honor this method by checking
self.abort_requested
periodically in theirworker.work
method, and exiting ifTrue
.- Return type
-
run
()¶ Start the worker.
The end-user should never need to call this function. But it cannot be made private or renamed, since it is called by Qt.
The order of method calls when starting a worker is:
calls QThreadPool.globalInstance().start(worker) | triggered by the QThreadPool.start() method | | called by worker.run | | | V V V worker.start -> worker.run -> worker.work
This is the function that actually gets called when calling
QThreadPool.start(worker)()
. It simply wraps thework()
method, and emits a few signals. Subclasses should NOT override this method (except with good reason), and instead should implementwork()
.
-
setAutoDelete
(self, bool)¶
-
start
()¶ Start this worker in a thread and add it to the global threadpool.
The order of method calls when starting a worker is:
calls QThreadPool.globalInstance().start(worker) | triggered by the QThreadPool.start() method | | called by worker.run | | | V V V worker.start -> worker.run -> worker.work
-
work
()[source]¶ Core event loop that calls the original function.
Enters a continual loop, yielding and returning from the original function. Checks for various events (quit, pause, resume, etc…). (To clarify: we are creating a rudimentary event loop here because there IS NO Qt event loop running in the other thread to hook into)