python 3.3.4 : python-daemon-3K ; How to use runner

标签 python daemon python-daemon

努力尝试让 python 守护进程使用 Python 3.3.4 工作。我正在使用来自 PyPi 的最新版本的 python-daemon-3K,即 1.5.8

起点是找到以下代码How do you create a daemon in Python?我认为是 2.x Python 的代码。

import time
from daemon import runner

class App():
    def __init__(self):
        self.stdin_path = '/dev/null'
        self.stdout_path = '/dev/tty'
        self.stderr_path = '/dev/tty'
        self.pidfile_path =  '/tmp/foo.pid'
        self.pidfile_timeout = 5
    def run(self):
        while True:
            print("Howdy!  Gig'em!  Whoop!")
            time.sleep(10)

app = App()
daemon_runner = runner.DaemonRunner(app)
daemon_runner.do_action()

尝试运行它时出现以下错误。

python mydaemon.py start
Traceback (most recent call last): File "mydaemon.py", line 60, in daemon_runner = runner.DaemonRunner(app) File "/depot/Python-3.3.4/lib/python3.3/site-packages/python_daemon_3K-1.5.8-py3.3.egg/daemon/runner.py", line 89, in init app.stderr_path, 'w+', buffering=0) ValueError: can't have unbuffered text I/O

任何指针如何转换为与 Python 3.3.4 一起工作或在 python-daemon-3K 中使用运行器的一个很好的例子

谢谢 德里克

最佳答案

要使代码在 python3 中运行,您需要在 DaemonRunner 类中进行更改,您不能使用无缓冲的文本 IO,但可以使用无缓冲的字节 IO,因此请将模式更改为 ' wb+' 将起作用:

class DaemonRunner(object):

        self.parse_args()
        self.app = app
        self.daemon_context = DaemonContext()
        self.daemon_context.stdin = open(app.stdin_path, 'r') 
        # for linux /dev/tty must be opened without buffering and with b
        self.daemon_context.stdout = open(app.stdout_path, 'wb+',buffering=0)
        # w+ -> wb+
        self.daemon_context.stderr = open(
            app.stderr_path, 'wb+', buffering=0)

关于 python 3.3.4 : python-daemon-3K ; How to use runner,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27506088/

相关文章:

python - 如何使用 setuptools 打包 Python 守护进程

python - 使用 dask 将过滤功能应用于数据 block

python - 替换/删除 pandas 数据中的某些文本?

python:打印数字,例如两个自然数的乘积

python - 带有自定义按钮的 QStyledItemDelegate

c - 如何使用简单的 Daemon C 代码在 Ubuntu 16.10 中启动 systemctl 服务

logging - 如何不创建 docker 容器日志?

linux - 仅在满足条件时才停止或重新启动的守护进程 Bash 脚本

python - 关闭 Python 守护进程时运行代码

Python 守护进程 : checking to have one daemon run at all times