- Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlesson07_kill_thread.py
More file actions
Latest commit
29 lines (27 loc) · 775 Bytes
/
Copy pathlesson07_kill_thread.py
File metadata and controls
29 lines (27 loc) · 775 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
# SuperFastPython.com
# example of killing a thread via its parent process
fromtimeimportsleep
frommultiprocessingimportProcess
# custom function to be executed in a new thread
deftask():
# run for ever
whileTrue:
# block for a moment
sleep(1)
# report a message
print('Task is running', flush=True)
# protect the entry point
if__name__=='__main__':
# create a new process with a new thread
process=Process(target=task)
# start the new process and new thread
process.start()
# wait a while
sleep(5)
# kill the new thread via the new process
print('Killing the thread via its process')
process.terminate()
# wait a while
sleep(2)
# all done
print('All done, stopping')