前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >自动重启挂掉的python脚本

自动重启挂掉的python脚本

作者头像
py3study
发布2020-01-10 10:41:16
4.1K0
发布2020-01-10 10:41:16
举报
文章被收录于专栏:python3python3

跑程序,因为内存问题或者其它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
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-07-24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档