栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何“停止”和“恢复”长时间运行的Python脚本?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何“停止”和“恢复”长时间运行的Python脚本?

这很简单,希望可以对您有所帮助:

import timeimport pickleREGISTRY = Nonedef main(start=0):    """Do some heavy work ..."""    global REGISTRY    a = start    while 1:        time.sleep(1)        a += 1        print a        REGISTRY = pickle.dumps(a)if __name__ == '__main__':    print "To stop the script execution type CTRL-C"    while 1:       start = pickle.loads(REGISTRY) if REGISTRY else 0        try: main(start=start)        except KeyboardInterrupt: resume = raw_input('If you want to continue type the letter c:') if resume != 'c':     break

运行示例:

$ python test.pyTo stop the script execution type CTRL-C123^CIf you want to continue type the letter c:c456789^CIf you want to continue type the letter c:$ python test.py


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/641669.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号