Python – Run Code in Thread

When writing a script or program it is often required to do more than one thing at the same time for efficiency or to not cause the program to lock up while completing a long task. In python you have 2 main ways to accomplish this, the multiprocessing library and the threading library the threading library is not true multiprocessing because of the GIL where the multiprocessing library truly allows you to preform 2 operations at the same time. I will not go in depth to all of the differences as that is a whole article by itself, but on a high level when parallel processing using the multiprocessing library a whole new process is spun up to run your code where in the threading library all of the child “threads” where your code runs is in the same process and access the same memory space. In this post I will be demonstrating a very easy way to utilize the threading library to run some code in an adjacent thread and returning data from that thread.


The Template Code

Below is a template that I often use when needing to utilize a thread, it is a subclass of the threading.Thread class in which we overwrite the “run” method to allow us to build an object to preform a task for us.

Looking through the comments you can see that the self.output attribute is where we will place the results of whatever we want this class to do. We can have more than 1 output variable if you need, the only thing that will change is how you pull data out of the thread upon completion.


How to Use the Template Code

So great we have a template for running code in a thread but how do we utilize it in our project? This is an oversimplified example but lets say that for some reason we want to count from 0 to 9 and wait 1 second in between each number and return a list containing 0 through 9 upon completion. Although not very practical this is a good example of where we would want to use a thread because lets say we have a GUI and don’t want it to hang up while we wait for it to get to 10, we need to put this in another thread.

Create More Methods

First thing we will want to do is create additional methods to help us accomplish what we need to do, I am going to throw in the counter method below.

This method will count from 0-9 waiting 1 second between each number, store each number in a list and returning that list

I named the method with an underscore to indicate to other developers that it should not be called directly by external code.

I will also modify the “run” method to execute the “_counter” method and place the output inside of the “self.output” attribute.

Which gives us the following class.


Executing the Thread

In order to run the code in our thread we need to add 2 lines which will instantiate our thread class into an object and then execute the “start” method. The start method belongs to the Thread class and will call the “run” method we have overwritten allowing our code to run.


So great we now have our thread running but now how do we get our output from the thread? This is where you will want to implement a watchdog loop which will watch for output or the completion of the thread and grab the output from the completed thread. Because of how my thread class is designed I know that the ‘output’ attribute will never be anything but ‘None’ unless the thread is completed so below is a watchdog that will watch for that attribute to be set to something other than none and grab the output once it sees it has changed.

As we can see it just keeps checking if there is something other than None in the “output” attribute and if there is not it will update the user that it is “Still Checking” and once it sees the output it grabs it and prints it out to the user.


Putting it all Together

So if we put all of this code above in a script that will run when we call the script we end up with the code below


What if I Want to Stop my Thread?

This example is not the greatest way of showing it because it is so basic but lets say for some reason we no longer need to complete the count from 0-9 and we want to explicitly kill the thread. This is very easy to implement by adding what I call a kill flag, we can set this to False by default and the configure the thread to continuously check that flag throughout its execution and if it is ever set to True it will stop executing. Below is our thread class modified to have that kill flag and a modified ‘_counter’ method to check for that kill flag.

We can see that before the “_counter” method appends our number to our output list it checks the status of the kill flag and if it is set to true it returns None which causes the method to stop executing. Its worth noting that we could return our partial list if we wanted but for this example I chose to just return None which would not modify the state of the output attribute.

Lazy Watchdog

Now I have modified my watchdog to only sit around and wait for 3 seconds and after that kill the thread and move on.

If we put this all together into a single script we get the file below.

If you copy this code into a file and run it you will see how it kills the thread and when it checks to see if it is alive it returns False. As I stated earlier this is very basic and your program will probably take input or data from the user or another part of the program when deciding to modify the kill flag and move on but you get the idea here with the mechanics of how you can pass information to and from your threads and change behavior based on that information.


I hope this article helped you understand about how to easily use threads in your programs. If you enjoyed this drop a comment and share on social media!


Be the first to comment

Leave a Reply

Your email address will not be published.