【问题标题】:Start Python program if it is not running如果 Python 程序没有运行,则启动它
【发布时间】:2017-03-03 01:27:02
【问题描述】:

我有一个 Python3 程序 B.py 我需要连续运行。 B.py 可能会抛出异常,当这种情况发生时,我想让它退出/关闭。 Pythong3 程序A.py 应该监视,如果B.py 已经退出,应该启动另一个B.py 实例。

我已经搜索过,但找不到一个干净的好方法。我不确定将 PID 写入文件是最好的方法(例如,文件会被锁定吗?)。

附带说明,我是 python 新手,所以请尽可能明确。

【问题讨论】:

    标签: python-3.x


    【解决方案1】:

    下面显示的程序旨在演示实现目标的一种非常简单的方法。


    A.py

    ​​>

    程序 A 的全部目的是让程序 B 一遍又一遍地运行。在此示例中指定了shell 参数,以便它可以轻松地在 Windows 和 Linux 上运行,假设程序 B 具有执行权限。如有需要,请随意扩展此程序。

    #! /usr/bin/env python3
    import subprocess
    
    
    def main():
        while True:
            subprocess.run('B.py', shell=True)
    
    
    if __name__ == '__main__':
        main()
    

    B.py

    ​​>

    该程序应替换为您自己的程序,但它演示了一个正在运行的程序,该程序会因随机异常而崩溃。 EXCEPTIONS 字符串直接从文档中复制而来,其余代码应该很容易理解。此程序的唯一用途是测试程序 A

    #! /usr/bin/env python3
    import builtins
    import random
    import time
    
    
    EXCEPTIONS = '''\
    BaseException
     +-- SystemExit
     +-- KeyboardInterrupt
     +-- GeneratorExit
     +-- Exception
          +-- StopIteration
          +-- StopAsyncIteration
          +-- ArithmeticError
          |    +-- FloatingPointError
          |    +-- OverflowError
          |    +-- ZeroDivisionError
          +-- AssertionError
          +-- AttributeError
          +-- BufferError
          +-- EOFError
          +-- ImportError
               +-- ModuleNotFoundError
          +-- LookupError
          |    +-- IndexError
          |    +-- KeyError
          +-- MemoryError
          +-- NameError
          |    +-- UnboundLocalError
          +-- OSError
          |    +-- BlockingIOError
          |    +-- ChildProcessError
          |    +-- ConnectionError
          |    |    +-- BrokenPipeError
          |    |    +-- ConnectionAbortedError
          |    |    +-- ConnectionRefusedError
          |    |    +-- ConnectionResetError
          |    +-- FileExistsError
          |    +-- FileNotFoundError
          |    +-- InterruptedError
          |    +-- IsADirectoryError
          |    +-- NotADirectoryError
          |    +-- PermissionError
          |    +-- ProcessLookupError
          |    +-- TimeoutError
          +-- ReferenceError
          +-- RuntimeError
          |    +-- NotImplementedError
          |    +-- RecursionError
          +-- SyntaxError
          |    +-- IndentationError
          |         +-- TabError
          +-- SystemError
          +-- TypeError
          +-- ValueError
          |    +-- UnicodeError
          |         +-- UnicodeDecodeError
          |         +-- UnicodeEncodeError
          |         +-- UnicodeTranslateError
          +-- Warning
               +-- DeprecationWarning
               +-- PendingDeprecationWarning
               +-- RuntimeWarning
               +-- SyntaxWarning
               +-- UserWarning
               +-- FutureWarning
               +-- ImportWarning
               +-- UnicodeWarning
               +-- BytesWarning
               +-- ResourceWarning
    '''
    
    
    def main():
        time.sleep(random.uniform(1, 9))
        lines = EXCEPTIONS.splitlines()
        names = [line.rsplit(None, 1)[-1] for line in lines]
        raise getattr(builtins, random.choice(names))()
    
    
    if __name__ == '__main__':
        main()
    

    【讨论】:

      猜你喜欢
      • 2017-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-16
      • 1970-01-01
      • 2010-11-16
      • 2021-04-03
      相关资源
      最近更新 更多