-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_namingThreadsAndLogging
More file actions
41 lines (32 loc) · 912 Bytes
/
02_namingThreadsAndLogging
File metadata and controls
41 lines (32 loc) · 912 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
import logging
import threading
import time
logging.basicConfig(level=logging.DEBUG,
format='[%(levelname)s] (%(threadName)-10s) %(message)s',
)
def worker():
logging.debug('Starting')
time.sleep(2)
logging.debug('Exiting')
def my_service():
logging.debug('Starting')
time.sleep(3)
logging.debug('Exiting')
t = threading.Thread(name='my_service', target=my_service)
w = threading.Thread(name='worker', target=worker)
w2 = threading.Thread(target=worker)
w.start()
w2.start()
t.start()
# Code reaches here, but the program is not quit until all the threads are successfully completed.
print('Exit Main')
''' OUTPUT
Exit Main
[DEBUG] (worker ) Starting
[DEBUG] (Thread-1 ) Starting
[DEBUG] (my_service) Starting
[DEBUG] (Thread-1 ) Exiting
[DEBUG] (worker ) Exiting
[DEBUG] (my_service) Exiting
Process finished with exit code 0
'''