threads.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. __filename__ = "threads.py"
  2. __author__ = "Bob Mottram"
  3. __license__ = "AGPL3+"
  4. __version__ = "0.0.1"
  5. __maintainer__ = "Bob Mottram"
  6. __email__ = "bob@freedombone.net"
  7. __status__ = "Production"
  8. import threading
  9. import sys
  10. import trace
  11. import time
  12. class threadWithTrace(threading.Thread):
  13. def __init__(self, *args, **keywords):
  14. threading.Thread.__init__(self, *args, **keywords)
  15. self.killed = False
  16. def start(self):
  17. self.__run_backup = self.run
  18. self.run = self.__run
  19. threading.Thread.start(self)
  20. def __run(self):
  21. sys.settrace(self.globaltrace)
  22. self.__run_backup()
  23. self.run = self.__run_backup
  24. def globaltrace(self, frame, event, arg):
  25. if event == 'call':
  26. return self.localtrace
  27. else:
  28. return None
  29. def localtrace(self, frame, event, arg):
  30. if self.killed:
  31. if event == 'line':
  32. raise SystemExit()
  33. return self.localtrace
  34. def kill(self):
  35. self.killed = True