Created
January 7, 2025 11:47
-
-
Save alej0varas/08913fa455e72afded16e6a33e19d2f8 to your computer and use it in GitHub Desktop.
Behavior of Python `threading.Thread` when using `daemon=True`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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