python的APScheduler框架,如何接收键盘信号停止运行

啥也不说了,先贴代码:
defmonitor(client):
run_heartbeat=client.run.heartbeat
etc_rpc=client.etc.rpc
document=etc_rpc.find_one({'server':'taskmanager'})
rpc=document['host']+':'+document['port']
ifprocess_ok('taskmanager'):
run_heartbeat.update({'process':'taskmanager','rpc':rpc},{'$set':{'selftesttime':datetime.datetime.now().strftime('%Y-%m-%d%H:%M:%S'),'selftest':'ok'}})
logging.info("update:selftestis'ok'")
print"update:run_heartbeat.taskmanager.selftestis'ok'"
else:
run_heartbeat.update({'process':'taskmanager','rpc':rpc},{'$set':{'selftesttime':datetime.datetime.now().strftime('%Y-%m-%d%H:%M:%S'),'selftest':'processerror'}})
logging.info("update:selftestis'processerror'")
print"update:run_heartbeat.taskmanager.selftestis'processerror'"
if__name__=="__main__":
print"TaskMonitorbeginning......"
sched=Scheduler()
sched.daemonic=False
sched.add_interval_job(monitor,seconds=int(tasktime),args=[client])
sched.start()
功能:每隔tasktime秒就运行一次monitor函数
期望结果:在运行过程中通过CTRL+C停止
求大神解决办法!
繁花不似锦
浏览 337回答 2
2回答

梦里花落0921

安装一个signalhandler即可importsignal,osdefhandler(signum,frame):print"gotsignal",signumifsignum==signal.SIGINT:print"gotCtrl+C,wewillquit"os._exit(1)signal.signal(signal.SIGINT,handler)if__name__=='__main__':importtimewhileTrue:time.sleep(1)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript