Python Central HubSign in

Python Programming / Python Threading

Daemon Threads

Learn daemon threads, when to use them, and how they differ from non-daemon threads.

diagram a daemon thread does not get to finish mermaid
When the last non-daemon thread ends, the interpreter shuts down and kills every daemon thread wherever it happens to be -- no exception raised in it, no finally block run, no flush. That is right for a background heartbeat and wrong for anything writing a file.

What is a daemon thread?

A daemon thread is a background thread.

  • If only daemon threads are left running, Python can exit.
  • Non-daemon threads keep the program alive.

Example

Python
import threading
import time
 
 
def background():
    while True:
        print("background working...")
        time.sleep(0.5)
 
 
t = threading.Thread(target=background, daemon=True)
t.start()
 
time.sleep(2)
print("Main thread exiting")

When to use daemon threads

  • logging/reporting loops
  • background monitoring

When NOT to use them

Don’t use daemon threads for work that must finish (e.g., writing critical data).

sketch The moment the main thread exits p5.js
Two runs of the same worker, which needs one second and prints FINISHED at the end. The main thread starts it and immediately exits. Watch what happens at the instant main finishes: the non-daemon run keeps the process alive to the end, and the daemon run is cut off wherever it happens to be. Neither raises anything.

Check yourself

quizCheck yourself
  1. A daemon thread is halfway through writing a file when the main thread finishes. What happens?

    Show answer

    B — It is killed where it stands — no exception, no `finally`, no flush — Measured: the same worker printed FINISHED with `daemon=False` and printed nothing with `daemon=True`. Interpreter shutdown does not ask daemon threads to stop; it stops existing.

  2. When is a daemon thread the right choice?

    Show answer

    B — When abandoning the work half-done is harmless — A heartbeat, a metrics poller or a cache warmer can be abandoned safely. Anything holding a lock, writing a file, or with a record in flight cannot.

  3. How do you make sure a worker thread finishes before the program exits?

    Show answer

    B — Leave it non-daemon (the default) and `join()` it — A non-daemon thread keeps the process alive by itself; `join()` makes the wait explicit. There is no `stop()` — a thread must be asked to finish, usually via a `threading.Event` it checks.

  4. What is the cleanest way to tell a long-running thread to shut down?

    Show answer

    B — Give it a `threading.Event` it checks periodically — Cooperative shutdown is the only reliable kind. The thread checks the flag at a point where stopping is safe, then unwinds normally — running its `finally` blocks.

🧪 Try It Yourself

Exercise 1 – Create a Daemon Thread

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Non-Daemon vs Daemon

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Main Thread is Not a Daemon

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did