Python › Concurrency (asyncio/threading/multiprocessing)
How do you create and run a basic thread using the threading module?
BeginnerAnswer
Create a threading.Thread object, passing the target function (and optional args), then call .start() to begin execution concurrently, and .join() to wait for it to finish before continuing.
import threading
def greet(name):
print(f"Hello, {name}!")
thread = threading.Thread(target=greet, args=("Sam",))
thread.start()
thread.join() # waits for the thread to finishReal-world example Running a background task (like a periodic health check) concurrently with the main program's execution.