gpt4 book ai didi

Python3 创建守护进程

转载 作者:太空狗 更新时间:2023-10-30 01:37:24 25 4
gpt4 key购买 nike

我尝试用 Python 创建一个守护进程以在 Ubuntu 服务器上运行。下面的代码是我遇到问题的代码。

import sys
import time
import threading
import logging
import logging.handlers

from daemon import runner

class Main(object):
def run(self):
my_logger = logging.getLogger('NameGeneratorDeamon')
my_logger.setLevel(logging.DEBUG)
handler = logging.handlers.SysLogHandler(address=('192.168.0.69', 514),facility=LOG_DAEMON)
my_logger.addHandler(handler)
try:
my_logger.info('Started')
while True:
pass
except Exception as inst:
#Send error to syslog server
my_logger.critical(inst)

class App():
def __init__(self):
self.stdin_path = '/dev/null'
self.stdout_path = '/dev/null'
self.stderr_path = '/dev/null'
self.pidfile_path = '/tmp/foo.pid'
self.pidfile_timeout = 5
def run(self):
service = Main()
service.run()

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

运行代码时得到的错误信息如下:

   File "Main.py", line 35, in <module>
daemon_runner = runner.DaemonRunner(app)
File "/usr/local/lib/python3.4/dist-packages/daemon/runner.py", line 111, in __init__
self.daemon_context.stdout = open(app.stdout_path, 'w+t')
io.UnsupportedOperation: File or stream is not seekable.

有谁知道如何解决这个问题,或者您有更好的方法在 Python 中创建守护进程吗?

最佳答案

在我的例子中,这没有错误:

# vi /usr/local/lib/python3.5/dist-packages/daemon/runner.py 
# 118 -120
self.daemon_context = DaemonContext()
self.daemon_context.stdin = open(app.stdin_path, 'wb+',buffering=0)
self.daemon_context.stdout = open(app.stdout_path, 'wb+',buffering=0)
self.daemon_context.stderr = open(
app.stderr_path, 'wb+', buffering=0)

关于Python3 创建守护进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37532479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com