【问题标题】:Python 3.3.4: python-daemon-3K ; How to use runnerPython 3.3.4:python-daemon-3K;跑步者的使用方法
【发布时间】:2015-02-14 20:24:17
【问题描述】:

努力尝试让 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 启动
回溯(最近一次通话最后): 文件“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+', 缓冲=0) ValueError: can't have unbuffered text I/O

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

谢谢 德里克

【问题讨论】:

    标签: python daemon python-daemon


    【解决方案1】:

    要让代码在 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)
    

    【讨论】:

    • 我看到了warnings.warn(“‘runner’模块不是这个库支持的API。”,DeprecationWarning) DaemonRunner 是否被弃用?有任何替代 API
    【解决方案2】:

    要使代码在 python3 中运行,您需要更改 DaemonRunner

    class DaemonRunner(object):
        self.parse_args()
        self.app = app
        self.daemon_context = DaemonContext()
        self.daemon_context.stdin = open(app.stdin_path, 'r') 
        self.daemon_context.stdout = open(app.stdout_path, 'w+')
        self.daemon_context.stderr = open(app.stderr_path, 'w+')
    

    【讨论】:

    • 我看到了warnings.warn(“‘runner’模块不是这个库支持的API。”,DeprecationWarning) DaemonRunner 是否已被弃用?有任何替代 API
    猜你喜欢
    • 2012-04-01
    • 1970-01-01
    • 2012-09-16
    • 1970-01-01
    • 2013-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-10
    相关资源
    最近更新 更多