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

property abort_requested

Whether the worker has been requested to stop.

Return type

bool

autoDelete(self)bool
create(Callable[[], None])QRunnable
property is_paused

Whether the worker is currently paused.

Return type

bool

property is_running

Whether the worker has been started

Return type

bool

pause()[source]

Request to pause the worker.

Return type

None

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 their worker.work method, and exiting if True.

Return type

None

resume()[source]

Send a request to resume the worker.

Return type

None

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 the work() method, and emits a few signals. Subclasses should NOT override this method (except with good reason), and instead should implement work().

send(value)[source]

Send a value into the function (if a generator was used).

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
toggle_pause()[source]

Request to pause the worker if playing or resume if paused.

Return type

None

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)