Python minimal thread (threading.Thread) example

This is a really simple example of how to add an extra thread to your Python script, to run some task in the background:

from threading import Thread
import time

def extra_thread_function():
    while True:
        print("extra_thread is running")
        time.sleep(1)

extra_thread = threading.Thread(target=extra_thread_function)
extra_thread.start()

# TODO Your code for the main thread goes here!

# OPTIONAL: Wait for extra_thread to finish
extra_thread.join()