Python threading status tracker

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 –

You can read my answer there as well and upvote if the answer helps! 🙂

Now the question is how do we get the progress, the status and success/failed report of a thread.
You can achieve the same using global vars. However I will explain a better class based approach here.
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
        for i in range(1000):
            self.progress = i
        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!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s