epicyon/threads.py

49 lines
1.3 KiB
Python
Raw Normal View History

2019-06-30 16:36:58 +00:00
__filename__ = "threads.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
2019-08-29 13:35:29 +00:00
__version__ = "1.0.0"
2019-06-30 16:36:58 +00:00
__maintainer__ = "Bob Mottram"
__email__ = "bob@freedombone.net"
__status__ = "Production"
import threading
import sys
import trace
2019-06-30 16:50:43 +00:00
import time
2019-06-30 16:36:58 +00:00
class threadWithTrace(threading.Thread):
2019-09-03 10:24:15 +00:00
def __init__(self, *args, **keywords):
self._args, self._keywords = args, keywords
threading.Thread.__init__(self, *self._args, **self._keywords)
2019-06-30 16:36:58 +00:00
self.killed = False
def start(self):
self.__run_backup = self.run
self.run = self.__run
threading.Thread.start(self)
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
2019-09-03 10:24:15 +00:00
2019-09-03 11:10:53 +00:00
def clone(self,fn):
return threadWithTrace(target=fn, \
args=self._args, \
daemon=True)