def stop(self, wait=None): self.keepgoing = False if wait==None: return if not self.going: return True if ongoing.verbose: print repr(self),"is waiting to finish" deadline = prectime() + 1000.0 * wait while self.going and (wait < 0.0 or prectime() < deadline): sleep(0.001) if ongoing.verbose and self.going: print repr(self),"has not finished (timed out after %g seconds)" % wait return not self.going
def go(self, bg=True): class DoubleGoError(Exception): pass self.thread = None if bg: if self.going: raise DoubleGoError, 'already running' if ongoing.verbose: print repr(self),"is launching a thread" self.thread = thread.start_new_thread(self.go, (), {'bg':False}) return if ongoing.verbose: print repr(self),"is starting" self.going = True self.keepgoing = True self.starttime = prectime() try: self.core(*self.pargs, **self.kwargs) except KeyboardInterrupt: pass self.onfinish() self.going = False self.stoptime = prectime() self.thread = None if ongoing.verbose: print repr(self),"has finished"
def elapsed(self): if not self.going: return None return (prectime() - self.starttime) / 1000.0