我们从Python开源项目中,提取了以下2个代码示例,用于说明如何使用threading._Timer()。
def handleKilling(signal, frame): dbgLog(LOG_INFO, "SIG " + str(signal) + " caught, exiting") if LOG_OUTPUT == 'file': LOG_HANDLE.close() # Kill all timer threads for thr in threading.enumerate(): if isinstance(thr, threading._Timer): try: thr.cancel() except: pass # Clean up iptables ipt('-D FORWARD -j ' + IPT_CHAIN) subChains = re.findall(re.compile(IPT_CHAIN + '_[a-z,0-9]{20}'), ipt('-L ' + IPT_CHAIN)) ipt('-F ' + IPT_CHAIN) for chain in subChains: ipt('-F ' + chain) ipt('-X ' + chain) ipt('-X ' + IPT_CHAIN) if IP6_SUPPORT: ipt6('-D FORWARD -j ' + IPT_CHAIN) ipt6('-F ' + IPT_CHAIN) for chain in subChains: ipt6('-F ' + chain) ipt6('-X ' + chain) ipt6('-X ' + IPT_CHAIN) sys.exit(0) # Logs message to LOG_FNAME or tty
def waitForThreads(): util.DEBUG_LOG('Checking for any remaining threads') while len(threading.enumerate()) > 1: for t in threading.enumerate(): if t != threading.currentThread(): if t.isAlive(): util.DEBUG_LOG('Waiting on: {0}...'.format(t.name)) if isinstance(t, threading._Timer): t.cancel() t.join() elif isinstance(t, threadutils.KillableThread): t.kill(force_and_wait=True) else: t.join()