Skip to content

Instantly share code, notes, and snippets.

@alej0varas
Created January 7, 2025 11:47
Show Gist options
  • Save alej0varas/08913fa455e72afded16e6a33e19d2f8 to your computer and use it in GitHub Desktop.
Save alej0varas/08913fa455e72afded16e6a33e19d2f8 to your computer and use it in GitHub Desktop.
Behavior of Python `threading.Thread` when using `daemon=True`
from threading import Thread
from time import sleep
def target_f():
while True:
print("start")
sleep(10)
print(
"You'll never see this message because the main function will exit before"
)
# IMPORTANT! Setting `daemon` to `True` allows us to stop the thread
# abruptly. If not it will run forever!
t = Thread(target=target_f, args=(), daemon=True)
def main():
while True:
t.start()
print("good night")
sleep(3)
print("good bye")
exit()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment