守护进程 python 项目,使用 Twisted

发布于 2024-12-11 03:10:18 字数 384 浏览 0 评论 0原文

如果我们使用 PEP-3143 及其参考实现 http://pypi.python.org/pypi/python -守护进程 那么看起来 Twisted 不可能工作,因为在守护进程期间,所有可能的文件处理程序都被显式关闭,其中包括管道。

当 Twisted 尝试调用 os.pipe() 并写入时,会得到错误的文件描述符。

据我所知,守护进程不适合通过此 PEP 进行联网? 也许这就是扭曲存在的原因

编辑:
我必须指出,问题更多的是“为什么 PEP 有效地使得创建网络应用程序变得不可能”,而不是“如何去做”。 Twisted 为了工作而打破了这条规则

If we use PEP-3143 and it's reference implementation http://pypi.python.org/pypi/python-daemon
then it looks like impossible to have Twisted working, since during daemonising ALL possible file handlers are explicitly closed, which includes pipes.

When Twisted tries to call os.pipe() and then write to it - gets bad file descriptor.

As I see it, daemonising is not suited for networking by this PEP?
And probably that's the reason why twisted exist

Edit:
I'll have to point out that the question is more of the "Why PEP effectively makes it impossible to create a network application" rather then "How to do it".
Twisted breaks this rules in order to work

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

踏月而来 2024-12-18 03:10:18

它不会关闭所有打开的文件描述符:只是关闭files_preserve属性中不存在的文件描述符。您可能可以通过计算出唤醒器的 FD 和反应堆中所有打开的套接字,然后将其传递给 files_preserve 来强制其工作...但为什么要麻烦呢?只需使用twistd并拥有twistd daemonize本身即可。

更好的是,使用 twistd -n 并让您的进程受到其他系统工具的监控,并且根本不用担心守护进程。

It doesn't close all the open file descriptors: just the ones not in the files_preserve attribute. You could probably coerce this to work by figuring out the FD of the waker and all open sockets in the reactor and then passing that to files_preserve... but why bother? Just use twistd and have twisted daemonize itself.

Better yet, use twistd -n and let your process get monitored by some other system tool, and don't bother with daemonization at all.

妄司 2024-12-18 03:10:18

supervisord + 暴发户

supervisord + upstart

两相知 2024-12-18 03:10:18

关闭所有打开的文件描述符的做法是由于去恶魔化进程从父进程继承一些打开的文件的可能性的结果。例如,您可以在一个进程中打开数十个文件(例如使用 os.open()),或者然后调用继承它们的子进程。作为子进程,您可能没有一种简单的方法来了解父进程中哪些文件描述符有用(除非您将其与命令行参数一起传递),并且您当然不想要 stdin、stdout 或 stderr,所以在执行其他操作之前,关闭所有打开的文件是完全合理的。

然后,去守护进程将采取一些额外的步骤来成为守护进程(如 PEP 中所述)。

一旦进程完全脱离任何类型的终端,它就可以根据需要开始打开文件和连接。它将打开其日志文件、配置文件和网络连接。

其他人提到,twisted 通过 twistd 工具已经很好地完成了所有这些工作,并且您不需要使用额外的模块。如果您不想使用 twistd(出于某种原因),但您确实想使用twistd,则可以使用外部的东西,但您应该首先 deamonize,然后再导入最后是扭曲的应用程序代码和开放的网络连接的其余部分。

The practice of closing all open filedescriptors is an effect of the possibility that the deamonizing process inherits some open files from the parent process. For example, you can open dozens of files in one process (with, say, os.open()) or and then invoke a sub-process that inherits them. You probably don't have an easy way, as a subprocess, to know what filedescriptors are useful from the parent process (unless you pass that along with command line arguments), and you certainly don't want stdin, stdout or stderr, so its perfectly reasonable to, before doing anything else, close all open files.

Then a deamonizing process will take some additional steps to become a deamon (as laid out in the PEP).

Once the process is fully detached from any kind of terminal, it can start opening files and connections as it needs. It'll open its log files, its configuration files, and its network connections.

Others have mentioned that twisted, via the twistd tool already does a pretty good job of all of this, and you don't need to use an extra module. If you don't want to use twistd (for some reason) but you do want to use twisted, you could use something external, but you should deamonize first and then import twisted and the rest of your application code and open network connections last.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文