努力尝试使用Python 3.3.4让python守护进程工作。我使用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(最近一次调用最后一次): 文件“mydaemon.py”,第60行,in 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:不能有无缓冲的文本I / O
任何指针如何翻译以使用Python 3.3.4或在python-daemon-3K中使用runner的好例子
由于 德里克
答案 0 :(得分:4)
要使代码在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)
答案 1 :(得分:0)
要使代码在python3中运行,您需要在SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
String value1=sharedpreferences.getString("key1", null);
String value2=sharedpreferences.getString("key2", null);
类中进行更改
DaemonRunner