| by sourcematters | No comments

Python 3.7 // Multithreading Template

Multithreading applications used to be stressful, difficult, and took a lot of code to make something simple work. Python makes this quite easy. Using the small template below, you can simply “drop in” your code into the work() function and be on your way! You can set the number of threads you like by changing the “25” in the for loop as well.

I’ve used this structure for many things such as:

  • Reading/Manipulating large number of directories or files
  • Making API requests to websites
  • Performing image manipulation/saving in bulk
from threading import Thread

def work(i):
    print("Thread {} Checking In!".format(i))

if __name__ == "__main__":
    for i in range(25):
        t = Thread(target=work, args=(i,))
        t.start()

Leave a Reply