- Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlesson07_exit.py
More file actions
Latest commit
27 lines (25 loc) · 629 Bytes
/
Copy pathlesson07_exit.py
File metadata and controls
27 lines (25 loc) · 629 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
# SuperFastPython.com
# example of a thread stopping itself via calling sys.exit()
fromtimeimportsleep
fromthreadingimportThread
importsys
# custom function to be executed in a new thread
deftask():
# wait a moment
sleep(1)
# report a message
print('Thread exiting now')
# exit the thread
sys.exit(0)
# this is never reached
print('Can you see me?')
# protect the entry point
if__name__=='__main__':
# create a new thread
thread=Thread(target=task)
# start the thread
thread.start()
# wait a moment
sleep(2)
# report a message
print('Main all done')