Created
June 16, 2014 04:32
-
-
Save sanfx/f2c9d82f38d78d7cb5d4 to your computer and use it in GitHub Desktop.
A daemon thread is ended when it's parent thread has ended, a non-daemon thread will keep the parent thread alive until the non-daemon is finished.
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
import threading | |
import time | |
import logging | |
logging.basicConfig(level=logging.DEBUG, | |
format='(%(threadName)-10s) %(message)s', | |
) | |
def daemon(): | |
logging.debug('Starting') | |
time.sleep(2) | |
logging.debug('Exiting') | |
d = threading.Thread(name='daemon', target=daemon) | |
d.setDaemon(True) | |
def non_daemon(): | |
logging.debug('Starting') | |
logging.debug('Exiting') | |
t = threading.Thread(name='non-daemon', target=non_daemon) | |
d.start() | |
t.start() | |
d.join() | |
t.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment