Python threading.Thread minimal example

This example shows how to subclass and start a threading.Thread:

thread_example.py
#!/usr/bin/env python3
import threading

class MyThread(threading.Thread):
    def run(self):
        print("My thread!")

my_thread = MyThread()
my_thread.start()
print("Main thread")

This will print

thread_output.txt
My thread!
Main thread

(it might also print Main thread first, depending on what gets executed first)


Check out similar posts by category: Python