【问题标题】:python process wrapperpython进程包装器
【发布时间】:2011-09-01 16:58:59
【问题描述】:

我需要在 python 中编写一个进程包装器,它将启动一个应用程序并在它失败时重新启动它。这将被记录到从命令行传入的指定日志位置。这可能吗?

【问题讨论】:

标签: python process


【解决方案1】:
>>> from subprocess import Popen
>>> def spawner(cmd_list):
...     while True:
...         print "Running proc..."
...         mon_proc = Popen(cmd_list)
...         print "Proc exit: %s" % mon_proc.wait()
... 
>>> spawner(['/bin/sleep', '3'])
Running proc...
Proc exit: 0
Running proc...
Proc exit: 0
Running proc...
Proc exit: 0
Running proc...

【讨论】:

    【解决方案2】:

    使用subprocess 模块。使用 Popen 启动它并获得一个 Popen 对象。根据您的需要,使用 Popen.poll() 或等待获取进程状态。循环执行,并使用logging 模块记录。

    我的 2 美分

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-20
      • 1970-01-01
      相关资源
      最近更新 更多