自动重启挂掉的python脚本
短信预约 -IT技能 免费直播动态提醒
跑程序,因为内存问题或者其它blabla问题(总之不是代码问题),程序可能会偶尔挂掉,我们又不能整天盯着程序,怎么办呢?写个脚本来检查程序是否挂掉,如果挂掉就重启,这是一个不错的想法,具体做法依操作系统而不同。
方法1
在linux下可以新建一个名为run.sh的脚本:
#!/bin/sh
while [ 1 ]; do
python program.py --params
done
在命令行中这样启动:
sh run.sh
其中program.py是要运行的python脚本,–params是参数。
在windows下可以类似地建个bat文件,由于bat不太熟,所以这部分就先空着。
方法2
在python中增加一些额外检查异常的代码,如果发生异常,就重新执行,这里用的是递归的方法。下面的例子中,我设置count最大为3,为了避免无限递归下去。
import time
count = 0
def compute_number():
for i in xrange(10):
print 'count number: %s' % str(i+1)
time.sleep(1)
raise Exception("a", "b")
def main():
print "AutoRes is starting"
print "Respawning"
global count
if count < 3:
try:
count += 1
compute_number()
except Exception, e:
print e
main()
finally:
print 'success'
if __name__ == "__main__":
main()
方法3
从这里 借鉴的做法:
#!/usr/bin/env python
import os, sys, time
def main():
print "AutoRes is starting"
executable = sys.executable
args = sys.argv[:]
args.insert(0, sys.executable)
time.sleep(1)
print "Respawning"
os.execvp(executable, args)
if __name__ == "__main__":
main()
多线程
另外还做了个多线程的小实验,大家可以看看。
import time
from multiprocessing import Pool
count = 0
def compute_number(num):
for i in xrange(3):
print 'current process = %s, count number: %s' % (str(num),str(i+1))
time.sleep(1)
raise Exception("a", "b")
def main(num):
print '===================='
print "current process = %d" % num
print "Respawning"
global count
if count < 2:
try:
count += 1
compute_number(num)
except Exception, e:
print e
main(num)
finally:
print 'success'
if __name__ == "__main__":
pool = Pool(2)
pool.map(main,[1,2])
pool.close()
pool.join()
输出如下:
====================
current process = 1
Respawning
current process = 1, count number: 1
====================
current process = 2
Respawning
current process = 2, count number: 1
current process = 1, count number: 2
current process = 2, count number: 2
current process = 1, count number: 3
current process = 2, count number: 3
('a', 'b')
====================
current process = 1
Respawning
current process = 1, count number: 1
('a', 'b')
====================
current process = 2
Respawning
current process = 2, count number: 1
current process = 1, count number: 2
current process = 2, count number: 2
current process = 1, count number: 3
current process = 2, count number: 3
('a', 'b')
====================
current process = 1
Respawning
success
success
('a', 'b')
====================
current process = 2
Respawning
success
success
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341