努力尝试让 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/