Skip to content

Instantly share code, notes, and snippets.

@sanfx
Created June 16, 2014 04:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sanfx/f2c9d82f38d78d7cb5d4 to your computer and use it in GitHub Desktop.
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.
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