-
Notifications
You must be signed in to change notification settings - Fork 4
/
thread_with_trace.py
44 lines (37 loc) · 1.28 KB
/
thread_with_trace.py
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
"""
Code from geeksforgeeks.org (edited)
Full link to the code: https://www.google.com/amp/s/www.geeksforgeeks.org/python-different-ways-to-kill-a-thread/amp
"""
from crash_window import Crash_window
import sys
import traceback
import trace
import threading
class Thread_with_trace(threading.Thread):
def __init__(self, *args, **keywords):
threading.Thread.__init__(self, *args, **keywords)
self.killed = False
def start(self):
try:
self.__run_backup = self.run
self.run = self.__run
threading.Thread.start(self)
except: # to show an error message if something wrong happens here
error_window = Crash_window(traceback.format_exc())
error_window.openWindow()
def __run(self):
sys.settrace(self.globaltrace)
self.__run_backup()
self.run = self.__run_backup
def globaltrace(self, frame, event, arg):
if event == 'call':
return self.localtrace
else:
return None
def localtrace(self, frame, event, arg):
if self.killed:
if event == 'line':
raise SystemExit()
return self.localtrace
def kill(self):
self.killed = True