自动重启挂掉的python脚本

跑程序,因为内存问题或者其它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
  • 5
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值