threading and Thread
standardintermediatethreading.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().
Think of it as
Thread(target=fn) is writing a job description but not hiring yet. .start() is hiring — the worker begins immediately, in parallel with you. .join() is waiting at the door until that worker reports back before you continue.
What we're doing: Start two threads that each print before and after a delay, and join both, showing .start() returns immediately while .join() actually waits.
- 9
- t1.start() launches the thread and returns immediately — it does not wait for download to finish.
- 11
- This line runs right after both starts, before either thread necessarily finishes — proof start() does not block.
- 12
- t1.join() blocks main until t1 specifically finishes; t2.join() then waits for t2.
file-A: starting
file-B: starting
main: both started, main keeps running
file-A: done
file-B: done
main: both joined, safe to continueWhy this works: .start() hands the function off to a new OS thread and returns control to main immediately — that is why "main: both started" prints before either "done" line. .join() is what actually blocks, giving the caller a way to wait for a specific thread instead of guessing how long it takes.
Reading a thread's result before calling .join()
Wrong
Better
What you see: Inconsistent output — sometimes None, sometimes 42 — because start() does not wait for the thread to finish before returning.
Why: start() only guarantees the thread has begun, not that it has completed. Reading shared state right after start() races against the new thread actually running — join() is what actually establishes "this thread is done" before the next line executes.
Thread — the methods worth knowing
Together
Remember: Thread(target=fn).start() begins running immediately and returns right away; .join() is what actually waits for it to finish.
See also: concurrency vs parallelism · lock and rlock · race conditions deadlocks and starvation

