高效的Python守护进程

时间:2011-01-09 02:58:21

标签: python daemon

我很好奇你如何在后台运行python脚本,每60秒重复一次任务。我知道你可以使用&在后台放一些东西,对这种情况有效吗?

我正在考虑做一个循环,让它等待60秒然后重新加载它,但有些事情让人感觉不舒服。

3 个答案:

答案 0 :(得分:94)

不要编写自己的守护进程,而是使用python-daemon代替! python-daemon实现了PEP 3143,“标准守护进程库”的良好守护程序规范。

我已经根据这个问题的接受答案包含了示例代码,即使代码看起来几乎相同,但它有一个重要的根本区别。如果没有python-daemon,则必须使用&将您的流程置于后台nohup,以防止您的进程在退出shell时被杀死。相反,当您运行程序时,它将自动从终端分离。

例如:

import daemon
import time

def do_something():
    while True:
        with open("/tmp/current_time.txt", "w") as f:
            f.write("The time is now " + time.ctime())
        time.sleep(5)

def run():
    with daemon.DaemonContext():
        do_something()

if __name__ == "__main__":
    run()

实际运行它:

python background_test.py

请注意此处缺少&

此外,this other stackoverflow answer详细说明了使用python-daemon的许多好处。

答案 1 :(得分:8)

我认为你的想法几乎就是你想要的。例如:

import time

def do_something():
    with open("/tmp/current_time.txt", "w") as f:
        f.write("The time is now " + time.ctime())

def run():
    while True:
        time.sleep(60)
        do_something()

if __name__ == "__main__":
    run()

time.sleep(60)的调用将使您的程序进入睡眠状态60秒。当该时间结束时,操作系统将唤醒您的程序并运行do_something()功能,然后将其重新置于睡眠状态。当你的程序正在睡觉时,它没有什么效果。这是编写后台服务的一般模式。

要从命令行实际运行此命令,您可以使用&:

$ python background_test.py &

执行此操作时,脚本中的任何输出都将转到与您启动它相同的终端。您可以重定向输出以避免这种情况:

$ python background_test.py >stdout.txt 2>stderr.txt &

答案 2 :(得分:6)

使用&在Greg描述的shell中可能是最简单的方式。

如果你真的想要创建一个强大的守护进程,你需要查看os.fork()命令。

来自Wikipedia的示例:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os, time

def createDaemon():
  """ 
      This function create a service/Daemon that will execute a det. task
  """

  try:
    # Store the Fork PID
    pid = os.fork()

    if pid > 0:
      print 'PID: %d' % pid
      os._exit(0)

  except OSError, error:
    print 'Unable to fork. Error: %d (%s)' % (error.errno, error.strerror)
    os._exit(1)

  doTask()

def doTask():
  """ 
      This function create a task that will be a daemon
  """

  # Open the file in write mode
  file = open('/tmp/tarefa.log', 'w')

  # Start the write
  while True:
    print >> file, time.ctime()
    file.flush()
    time.sleep(2)

  # Close the file
  file.close()

if __name__ == '__main__':

  # Create the Daemon
  createDaemon()

然后你可以在doTask()区块中放置所需的任何任务。

你不需要使用&来启动它,它可以让你进一步自定义执行。