Python 中守护进程的工作实现
有谁知道使用 python 实现守护进程的工作和有据可查吗?如果您知道符合这两个要求的项目,请在此处发布链接。
Does anyone know of a working and well documented implementation of a daemon using python? Please post a link here if you know of a project that fits these two requirements.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我能想到的三个选项 -
multiprocessing
模块。尝试分叉进程等的实质内容隐藏在这个实现中。非常整洁。我不会推荐 2 或 3 个,因为你实际上是在重复 cron 功能。 Linux系统范式是让多个简单的工具交互并解决你的问题。除非有其他原因需要创建守护程序(除了定期触发),否则请选择其他方法。
另外,如果您在循环中使用 daemonize 并且发生崩溃,请确保您有可以帮助您调试的日志。还要设计一种方法让脚本重新开始。而如果将脚本添加为 cron 作业,它将在您保留的时间间隔内再次触发。
Three options I can think of-
multiprocessing
module. The nitty-gritty of trying to fork a process etc. are hidden in this implementation. It's pretty neat.I wouldn't recommend 2 or 3 'coz you're in fact repeating cron functionality. The Linux system paradigm is to let multiple simple tools interact and solve your problems. Unless there are additional reasons why you should make a daemon (in addition to trigger periodically), choose the other approach.
Also, if you use daemonize with a loop and a crash happens, make sure that you have logs which will help you debug. Also devise a way so that the script starts again. While if the script is added as a cron job, it will trigger again in the time gap you kept.
如果您只想运行守护进程,请考虑 Supervisor,它是一个本身控制和管理守护进程的守护进程。
如果你想了解具体情况,可以查看 Supervisor 的启动脚本或对此lazyweb 请求的一些响应。
If you just want to run a daemon, consider Supervisor, a daemon that itself controls and manages daemons.
If you want to look at the nitty-gritty, you can check out Supervisor's launch script or some of the responses to this lazyweb request.
检查此链接以获取双叉守护进程:http: //code.activestate.com/recipes/278731-creating-a-daemon-the-python-way/
代码可读且文档齐全。您想查看 W. Richard 的《UNIX 环境中的高级编程》一书的第 13 章,了解有关 Unix 守护进程的详细信息。
Check this link for a double-fork daemon: http://code.activestate.com/recipes/278731-creating-a-daemon-the-python-way/
The code is readable and well-documented. You want to take a look at chapter 13 of W. Richard's book 'Advanced Programming in the UNix Environment' for detailed information on Unix daemons.