如何在Python中创建守护程序? [英] How do you create a daemon in Python?

查看:83
本文介绍了如何在Python中创建守护程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Google上搜索会显示x2代码段。第一个结果是此代码食谱,其中包含许多文档和说明,以及一些下面的有用讨论。

Searching on Google reveals x2 code snippets. The first result is to this code recipe which has a lot of documentation and explanation, along with some useful discussion underneath.

但是,另一个代码示例,虽然没有包含太多文档,但包括用于传递命令(例如启动,停止和重新启动)的示例代码。它还创建了一个PID文件,可以方便地检查守护程序是否已在运行中。

However, another code sample, whilst not containing so much documentation, includes sample code for passing commands such as start, stop and restart. It also creates a PID file which can be handy for checking if the daemon is already running etc.

这些示例都说明了如何创建守护程序。还有其他需要考虑的事情吗?一个样本比另一个样本好吗?为什么?

These samples both explain how to create the daemon. Are there any additional things that need to be considered? Is one sample better than the other, and why?

推荐答案

当前解决方案

PEP 3143(标准守护进程)的参考实现库)现在可以作为 python-daemon 使用。

历史答案

Sander Marechal的代码示例优于最初于2004年发布的原始代码。曾经为Pyro贡献了一个守护进程,但是如果我不得不做的话,可能会使用Sander的代码。

Sander Marechal's code sample is superior to the original, which was originally posted in 2004. I once contributed a daemonizer for Pyro, but would probably use Sander's code if I had to do it over.

这篇关于如何在Python中创建守护程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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