为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

繁花不似锦 2019-04-16 17:05:34
啥也不说了,先贴代码:defmonitor(client):run_heartbeat=client.run.heartbeatetc_rpc=client.etc.rpcdocument=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=Falsesched.add_interval_job(monitor,seconds=int(tasktime),args=[client])sched.start()功能:每隔tasktime秒就运行一次monitor函数期望结果:在运行过程中通过CTRL+C停止求大神解决办法!
查看完整描述

2 回答

?
梦里花落0921

TA贡献1772条经验 获得超6个赞

安装一个signalhandler即可
importsignal,os
defhandler(signum,frame):
print"gotsignal",signum
ifsignum==signal.SIGINT:
print"gotCtrl+C,wewillquit"
os._exit(1)
signal.signal(signal.SIGINT,handler)
if__name__=='__main__':
importtime
whileTrue:
time.sleep(1)
                            
查看完整回答
反对 回复 2019-04-16
  • 2 回答
  • 0 关注
  • 336 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信