我如何运行长期(无限)的Python程序? [英] How do I run long term (infinite) Python processes?

查看:1293
本文介绍了我如何运行长期(无限)的Python程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用Python进行Web开发试验。到目前为止,我已经使用Apache与mod_wsgi的和为Python 2.7 Django的Web框架一定的成功。不过,我已经具有流程不断磨合,更新信息和这样碰到一些问题。

I've recently started experimenting with using Python for web development. So far I've had some success using Apache with mod_wsgi and the Django web framework for Python 2.7. However I have run into some issues with having processes constantly running, updating information and such.

我已经写了一个脚本,我称之为daemonManager.py,可以启动和停止全部或单个蟒蛇更新循环(我应该称他们为守护程序?)。它是通过分叉,然后加载它应该运行特定功能的模块,并开始一个无限循环。它保存在一个PID文件 / var / run中来跟踪的过程。到现在为止还挺好。我遇到的问题是:

I have written a script I call "daemonManager.py" that can start and stop all or individual python update loops (Should I call them Daemons?). It does that by forking, then loading the module for the specific functions it should run and starting an infinite loop. It saves a PID file in /var/run to keep track of the process. So far so good. The problems I've encountered are:


  • 现在,然后程序将只是退出之一。我检查 PS 在早晨和过程只是走了。已记录没有错误(我使用了登录模块),而且我几乎涵盖所有例外,我能想到的和记录它们。此外,我不认为这些戒烟过程中有什么与我的code,因为我所有的进程中运行完全不同的code和出口在pretty类似的区间。我可能是错的,当然。它是正常的,他们已经运行天/周的Python程序只是死吗?我应该如何解决这个问题?我应该写另一个守护进程,定期检查,如果其他后台程序仍然在运行?如果该守护进程停止?我在如何处理这一损失。

  • Now and then one of the processes will just quit. I check ps in the morning and the process is just gone. No errors were logged (I'm using the logging module), and I'm covering every exception I can think of and logging them. Also I don't think these quitting processes has anything to do with my code, because all my processes run completely different code and exit at pretty similar intervals. I could be wrong of course. Is it normal for Python processes to just die after they've run for days/weeks? How should I tackle this problem? Should I write another daemon that periodically checks if the other daemons are still running? What if that daemon stops? I'm at a loss on how to handle this.

我如何编程知道,如果一个进程仍在运行或不?我节省了PID文件 / var / run中并检查是否PID文件是存在的,确定的过程中是否运行。但如果进程只是在死亡意想不到的原因,PID文件将保持不变。所以我每次都删除这些文件的进程崩溃(一对夫妇每周次),其中排序失​​败的目的。我想,如果一个进程的PID文件中运行的我可以检查,但如果另一个进程已经开始,被分配死者进程的PID?我的守护进程会认为该进程正在运行的罚款,即使它早就死了。我又不知所措只是如何处理这个问题。

How can I programmatically know if a process is still running or not? I'm saving the PID files in /var/run and checking if the PID file is there to determine whether or not the process is running. But if the process just dies of unexpected causes, the PID file will remain. I therefore have to delete these files every time a process crashes (a couple of times per week), which sort of defeats the purpose. I guess I could check if a process is running at the PID in the file, but what if another process has started and was assigned the PID of the dead process? My daemon would think that the process is running fine even if it's long dead. Again I'm at a loss just how to deal with this.

如何最佳运行无限Python的过程中,也有希望在脱落上述问题的一些光的任何有用的答案,我会接受

Any useful answer on how to best run infinite Python processes, hopefully also shedding some light on the above problems, I will accept

我使用的是Ubuntu的机器上的Apache 2.2.14。结果
我的Python版本2.7.2是

I'm using Apache 2.2.14 on an Ubuntu machine.
My Python version is 2.7.2

推荐答案

我将指出这是的有一个的方式来管理一个长期运行的进程(LRP)打开任何舒展。

I'll open by stating that this is one way to manage a long running process (LRP) -- not de facto by any stretch.

在我的经验,最好的产品来自于专注于你正在处理特定的问题,同时委托技术支持向其他库。在这种情况下,我指的是一个后台进程(艺术双叉的),监测的行为,并记录重定向。

In my experience, the best possible product comes from concentrating on the specific problem you're dealing with, while delegating supporting tech to other libraries. In this case, I'm referring to the act of backgrounding processes (the art of the double fork), monitoring, and log redirection.

我最喜欢的解决方案是 http://supervisord.org/

My favorite solution is http://supervisord.org/

使用像supervisord的系统,你基本上编写执行任务,而停留在一个无限循环的常规python脚本。

Using a system like supervisord, you basically write a conventional python script that performs a task while stuck in an "infinite" loop.

#!/usr/bin/python

import sys
import time

def main_loop():
    while 1:
        # do your stuff...
        time.sleep(0.1)

if __name__ == '__main__':
    try:
        main_loop()
    except KeyboardInterrupt:
        print >> sys.stderr, '\nExiting by user request.\n'
        sys.exit(0)

编写脚本这种方式使得它简单,方便开发和调试(你可以轻松地开始/停止它在终端,看着日志输出作为事件的展开)。当谈到时间投入到生产中,您只需定义监事配置调用脚本(这里的完全定义一个节目,其中大部分是可选的例子:<一href=\"http://supervisord.org/configuration.html#program-x-section-example\">http://supervisord.org/configuration.html#program-x-section-example).

Writing your script this way makes it simple and convenient to develop and debug (you can easily start/stop it in a terminal, watching the log output as events unfold). When it comes time to throw into production, you simply define a supervisor config that calls your script (here's the full example for defining a "program", much of which is optional: http://supervisord.org/configuration.html#program-x-section-example).

监督员具有的一堆配置选项,所以我不会一一列举,但我会说,它专门解决你所描述的问题的:


  • 一个后台/ Daemonizing

  • PID跟踪(可配置来重新启动一个进程,应该意外终止)

  • 日志通常你的脚本(流处理程序,如果使用日志模块,而不是打印),但让上司重定向到一个文件给你。

这篇关于我如何运行长期(无限)的Python程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆