54 lines
1.9 KiB
Python
54 lines
1.9 KiB
Python
import threading
|
|
|
|
|
|
class AsyncTask(threading.Thread):
|
|
"""
|
|
AsyncTask
|
|
|
|
AsyncTask is a utility class allowing to execute a task asynchronously in a thread.
|
|
For more informations read the constructor documentation.
|
|
|
|
@author Jérémi Nihart / EndMove
|
|
@link https://git.endmove.eu/EndMove/WebPicDownloader
|
|
@version 1.0.1
|
|
@since 2022-09-04
|
|
"""
|
|
# Variables
|
|
__run_callback = None
|
|
__run_args: list = None
|
|
__quite_callback = None
|
|
__quite_args = None
|
|
|
|
# Constructor
|
|
def __init__(self, rcallback, rargs=None, qcallback=None, qargs=None) -> None:
|
|
"""
|
|
Constructor
|
|
=> Indicate in the constructors, the parameters for launching the process, as
|
|
well as the stop otpions. Then use the function {AsyncTask.start()} to start
|
|
the thread and the processing.
|
|
[!]: The function {AsyncTask.run()} is reserved for the thread and should not be run
|
|
from outside.
|
|
|
|
* :rcallback: -> Asynchronous start function.
|
|
* :rargs: -> Arguments for the asyncrone startup function.
|
|
* :qcallback: -> Stop function to stop asynchronous processing.
|
|
* :qargs: -> Argument for the stop function.
|
|
"""
|
|
super().__init__()
|
|
self.__run_callback = rcallback
|
|
self.__run_args = rargs if rargs else ()
|
|
self.__quite_callback = qcallback if qcallback else lambda: print("exiting thread")
|
|
self.__quite_args = qargs if qargs else ()
|
|
|
|
def run(self) -> None:
|
|
"""
|
|
[Internal function of (threading.Thread)]
|
|
[!] : This function must not be used! Start the task with {AsyncTask.start()} !
|
|
"""
|
|
self.__run_callback(*self.__run_args)
|
|
|
|
def stop(self) -> None:
|
|
"""
|
|
Stop the running task, make sure you have previously defined the stop function.
|
|
"""
|
|
self.__quite_callback(*self.__quite_args) |