#!/usr/bin/env python #!coding=utf-8 from threading import Thread,Event,settrace,currentThread def globaltrace(frame,why,arg): return currentThread().localtrace if why=='call' else None class ExThread(Thread): def __init__(self,*args,**kw): Thread.__init__(self,*args,**kw) self._willKill=False self._event=Event() self._event.set() def start(self): settrace(globaltrace) Thread.start(self) def localtrace(self,frame,why,arg): if self._willKill and why=='line': raise SystemExit() elif not self._event.isSet() and why=='line': self._event.wait() return self.localtrace def kill(self): self._willKill=True def pause(self): self._event.clear() def ahead(self): self._event.set()