1.threading and Thread
threading.Thread(target=fn, args=...) creates a thread that will run fn. Nothing runs until .start() is called, and the caller can wait for it to finish with .join().
Key features
- Thread(target=fn, args=(...)) builds a thread object; it does not run yet
- .start() begins running fn on a separate OS thread
- .join() blocks the caller until that thread finishes
- Threads share the same memory as the process that created them
- threading.current_thread().name identifies which thread is running
Thread — the methods worth knowing
Command
import threading
t = threading.Thread(target=some_function, args=(arg1, arg2))
t.start() # begins running immediately
t.join() # blocks here until t finishesThread(target=fn).start() begins running immediately and returns right away; .join() is what actually waits for it to finish.