API Usage¶
AsyncManager¶
- class PySink.AsyncManager¶
Bases:
QObjectClass that manages all
workersand their corresponding threads. Once a worker is created, provide it to thestart_worker()method to start the worker’s long-running task. If the worker is of typeCancellableAsyncWorker, it can be cancelled by passing the worker’sidto thecancel_worker()method. All threads/workers will be garbage collected upon completion.Worker signals are also exposed via the provided signal attributes
worker_started_signal,worker_progress_signal, andworker_finished_signal. Once all active workers are complete, the manager will emit itsall_workers_finished_signal- all_workers_finished_signal¶
Signal(): Signals that all workers have finished their tasks.
- cancel_all_workers() {<class 'str'>: <class 'str'>}¶
Attempts to cancel all workers that are active and cancellable..
- Returns:
A dictionary of errors if they are encountered, keyed by worker id.
- Return type:
dict
- cancel_worker(worker_id: str) str¶
Attempts to cancel the worker with the given id…
- Parameters:
worker_id (str) – The unique identifier of the worker to be cancelled
- Returns:
An message describing the issue if the worker could not be cancelled or does not exist
- Return type:
str
- start_worker(worker: AsyncWorker) None¶
Starts the worker on a new thread (or queues the worker if there are no threads available). Once the worker is on the thread, the worker’s
run()method is called..- Parameters:
worker (AsyncWorker) – The worker to be run
- Raises:
Exception – Raised if a worker is provided that has the same id as one that is already running.
- worker_finished_signal¶
Signal(
AsyncWorkerResults): Signals that a worker has finished its task. Contains the results of the worker.
- worker_progress_signal¶
Signal(
AsyncWorkerProgress): Signal that contains progress data for a worker.
- worker_started_signal¶
Signal(str): Signals that a worker has started its task. Contains the id of the worker.
AsyncWorker¶
- class PySink.AsyncWorker(identifier: str | None = None)¶
Bases:
QRunnableA class that represents an Asynchronous Worker. Workers should inherit from this class and perform their long-running tasks by overriding the
run()method.To define custom
resultsandsignals, redefine them within your custom worker’s __init__ method..- Parameters:
identifier (str, optional) – A unique identifier to differentiate this worker from other workers. Defaults to a uuid4 string
- complete(**kwargs) None¶
Signals the completion of the worker’s long-running task. This should be called at the end of the overridden
run()method. Calling this method emits the worker’s results via theself.signals.finishedsignal.By default, the kwargs provided will be packaged into
results.results_dictas key-value pairs. However, if a custom result type was defined within__init__(), the key-word arguments will also be mapped to their corresponding result attributes (they will still be packed into the result_dict)- Parameters:
kwargs – Result values to be emitted, defined as key-word arguments
- emit_start() None¶
This method can be called within
run()to let the application know that the long-running task has begun (this is signalled via theself.signals.startedsignal). Calling this method is completely optional and does not affect the functionality of the worker.
- reset() None¶
Resets the worker’s state. All warnings and errors will be cleared, and
resultwill be reset to the defined result type.
- run() None¶
Performs the worker’s long-running task. Custom Workers should override this method. By default, this will perform a demo task of counting to 5 at a one-second interval.
- update_progress(progress_value: int, message='') None¶
Emits the progress value and message. These values are emitted via the
self.signals.progresssignal..- Parameters:
progress_value (int) – The current progress value. For discrete behavior, this value should be [0, 100]. For indeterminate behavior, this value should be -1.
message (str, optional) – A message describing the current progress stage of the worker (‘Downloading’, ‘Calculating’, etc).
CancellableAsyncWorker¶
- class PySink.CancellableAsyncWorker(*args, **kwargs)¶
Bases:
AsyncWorkerA class that represents a cancellable
AsyncWorker. Any workers that need to be cancellable should inherit from this class. CancellableAsyncWorker inherits fromAsyncWorker, and offers the ability to cancel the worker’s task at any time by calling thecancel()method.IMPORTANT NOTE: While calling
cancel()effectively halts the worker’s task, it DOES NOT terminate the execution ofrun()(doing so could result in unwanted data corruption). Withinrun(), you should poll thecancelledflag intermittently and return early if it is True.- cancel() None¶
Cancels the worker. A ‘Cancelled’ message is appended to both
warningsanderrors, and thefinishedsignal is emitted (similar to callingcomplete()). Once this method is called, all internal method calls/signal updates will be ignored, and an internal flag calledcancelledis set to True.IMPORTANT NOTE: While calling
cancel()effectively halts the worker’s task, it DOES NOT terminate the execution ofrun()(doing so could result in unwanted data corruption). Withinrun(), you should poll thecancelledflag intermittently and return early if it is True.
- complete(**kwargs) None¶
Signals the completion of the worker’s long-running task. This should be called at the end of the overridden
run()method. Calling this method emits the worker’s results via theself.signals.finishedsignal.By default, the kwargs provided will be packaged into
results.results_dictas key-value pairs. However, if a custom result type was defined within__init__(), the key-word arguments will also be mapped to their corresponding result attributes (they will still be packed into the result_dict)- Parameters:
kwargs – Result values to be emitted, defined as key-word arguments
- emit_start()¶
This method can be called within
run()to let the application know that the long-running task has begun (this is signalled via theself.signals.startedsignal). Calling this method is completely optional and does not affect the functionality of the worker.
- reset()¶
Resets the worker’s state. All warnings and errors will be cleared, and
resultwill be reset to the defined result type.
- update_progress(progress_value: int, message=None) None¶
Emits the progress value and message. These values are emitted via the
self.signals.progresssignal..- Parameters:
progress_value (int) – The current progress value. For discrete behavior, this value should be [0, 100]. For indeterminate behavior, this value should be -1.
message (str, optional) – A message describing the current progress stage of the worker (‘Downloading’, ‘Calculating’, etc).