It is useful to know how much work a thread has completed. Following are a few ways in which you can achieve this.
I was reading through stackoverflow and then I stumbled upon this question – https://stackoverflow.com/questions/55192979/how-to-track-progress-of-job-worker-threads-when-threads-are-initiated-from-a-jo/55193043#55193043
def __init__(self): self.progress = 0 self.success = True self.isDone = False self.error = "No Error Occurred"
We can declare a progress var to denote the amount of work done by the thread. We declare a “isDone” variable to denote whether the task was completed or not. We declare another variable “success” and “error” to capture the information whether task was successful or not.
This is how you can include above logic in your code.
def actualWork(self): self.isDone = 0 try: for i in range(1000): self.progress = i time.sleep(0.01) self.isDone = True except Exception as e: self.success = False self.error = str(e) def getProgress(self): return self.progress def getError(self): return self.error
I hope this makes your life in python multithreading easier. Happy Coding!