Python 3.3.4:python-daemon-3K; 如何使用跑步者

use*_*990 7 python daemon python-daemon

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

起点是以下代码:如何在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()
Run Code Online (Sandbox Code Playgroud)

试图运行此我得到以下错误.

python mydaemon.py start
Traceback(最近一次调用last):文件"mydaemon.py",第60行,在daemon_runner = runner.DaemonRunner(app)文件"/depot/Python-3.3.4/lib/python3.3/site -packages/python_daemon_3K-1.5.8-py3.3.egg/daemon/runner.py",第89行,在init app.stderr_path中,'w +',buffering = 0)ValueError:不能有无缓冲的文本I/O

任何指针如何翻译以使用Python 3.3.4或在python-daemon-3K中使用runner的好例子

谢谢德里克

Pad*_*ham 5

要使代码在 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)
Run Code Online (Sandbox Code Playgroud)