Fixed the program stop bug. Adapted webpic script for multi-tasking + added comments

This commit is contained in:
2022-09-04 15:28:28 +02:00
parent 7002439532
commit 03a6d9b54f
6 changed files with 267 additions and 172 deletions

View File

@@ -10,37 +10,45 @@ class AsyncTask(threading.Thread):
@author Jérémi Nihart / EndMove
@link https://git.endmove.eu/EndMove/WebPicDownloader
@version 1.0.0
@since 2022-09-01
@version 1.0.1
@since 2022-09-04
"""
# Variables
__stop = None
__callback = None
__args: list = None
__run_callback = None
__run_args: list = None
__quite_callback = None
__quite_args = None
# Constructor
def __init__(self, callback, args=()) -> None:
def __init__(self, rcallback, rargs=None, qcallback=None, qargs=None) -> None:
"""
Constructor
=> Spacify here the function that should be launched asynchronously. Then use the
function {AsyncTask.start()} to start the thread and the processing.
=> 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.
:callback: -> Is the function to launch asynchronously.
:args: -> Argument to pass to the function when executing it.
* :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.__stop = threading.Event()
self.__callback = callback
self.__args = args
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:
"""
[!] : This function should not be used! Start the task with {AsyncTask.start()}!
[Internal function of (threading.Thread)]
[!] : This function must not be used! Start the task with {AsyncTask.start()} !
"""
self.__callback(*self.__args)
self.__run_callback(*self.__run_args)
def stop(self) -> None:
# TODO
self.__stop.set()
"""
Stop the running task, make sure you have previously defined the stop function.
"""
self.__quite_callback(*self.__quite_args)