python守护进程 - python-daemon
2023-06-13 09:16:16 时间
python的守护进程模块,遵循PEP 3143,很好很强大!
install
# https://pypi.python.org/pypi/python-daemon/
sudo pip install python-daemon
Usage
import logging
import time
# third party libs
from daemon import runner
class Updater():
def __init__(self):
self.stdin_path = '/dev/null'
self.stdout_path = '/dev/tty'
self.stderr_path = '/dev/tty'
self.pidfile_path = '/tmp/testdaemon.pid'
self.pidfile_timeout = 5
def run(self):
while True:
# Main code goes here ...
# Note that logger level needs to be set to logging.DEBUG before this shows up in the logs
logger.debug("Debug message")
logger.info("Info message")
logger.warn("Warning message")
logger.error("Error message")
time.sleep(10)
app = Updater()
logger = logging.getLogger("DaemonLog")
logger.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
handler = logging.FileHandler("/tmp/testdaemon.log")
handler.setFormatter(formatter)
logger.addHandler(handler)
daemon_runner = runner.DaemonRunner(app)
# This ensures that the logger file handle does not get closed during daemonization
daemon_runner.daemon_context.files_preserve = [handler.stream]
daemon_runner.do_action()
Refer:
Create a daemon on Ubuntu Building a python daemon process How do you use python-daemon the way that it's documentation dictates?
相关文章
- Python 命令行参数的3种传入方式
- python详细安装教程环境配置_Python配置环境
- Python 人工智能 5秒钟偷走你的声音
- mt4 python_一个使用Python自动化交易外汇MT4脚本实现「建议收藏」
- 手把手教你用Python破解邻家小妹wifi密码
- python删除首行_Python删除文件第一行
- Python入门系列(六)一篇学会python函数
- 学Python,不需要有编程基础! (二)Python能干什么?[通俗易懂]
- Python进程管理
- python监控网页内容变化_使用Python监控文件内容变化代码实例
- Python元组_python元组的定义方式
- python语言一般用于什么_PYthon
- 【说站】python探针如何实现
- Python学习-Chapter7 用户输入和while循环
- 使用Python批量实现某一Excel文件中每3行数据存一个Excel文件
- python装饰器
- 【测试开发】python系列教程:Python注释和解释器
- python-Python与SQLite数据库-SQLite数据库的基本知识(一)
- python-Python与SQLite数据库-处理SQLite查询结果(一)
- python通过Tkinter显示网络图片详解编程语言
- python_Day38_多进程和multiprocess模块1详解编程语言
- 安装Python MySQL驱动之快速指南(python安装mysql驱动)
- 探索Linux中的Python图像处理库PIL(linuxpil)
- Python实现MySQL数据库的读取(python读取mysql)