-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_daemonThreadsAndJoins
More file actions
44 lines (31 loc) · 1008 Bytes
/
03_daemonThreadsAndJoins
File metadata and controls
44 lines (31 loc) · 1008 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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')
# Create a daemon thread. IT stays awake even after the main program has completed
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()
# join() waits for the thread to complete in the main program for the time specified in the argument block.
# Since we are sleeping for time more than that, the main program quits and the complete output is not visible from the daemon thread.
d.join(1)
print('d.isAlive()', d.isAlive())
t.join()
''' OUTPUT
(daemon ) Starting
(non-daemon) Starting
(non-daemon) Exiting
d.isAlive() True
Process finished with exit code 0
'''