首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何给python-daemon添加更多的命令参数?

如何给python-daemon添加更多的命令参数?
EN

Stack Overflow用户
提问于 2016-09-20 03:54:07
回答 1查看 372关注 0票数 2

我有一个基本的Python守护进程,使用旧版本的python-daemon和以下代码创建:

代码语言:javascript
复制
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()

现在一切正常,但我需要向我的守护进程再添加一个可能的命令。当前的默认命令是“启动、停止、重新启动”。我需要第四个命令"mycommand“,它在执行时只运行以下代码:

代码语言:javascript
复制
my_function()
print "Function was successfully run!"

我试过用谷歌搜索和研究,但我自己都搞不清楚。我试着用sys.argv手动提取参数,而不是陷入python守护程序代码中,但我不能让它工作。

EN

回答 1

Stack Overflow用户

发布于 2016-09-20 04:58:15

看一下runner模块的代码,下面的代码应该是有效的…我在定义stdout和stderr时遇到了问题...你能测试一下吗?

代码语言:javascript
复制
from daemon import runner
import time

# Inerith from the DaemonRunner class to create a personal runner
class MyRunner(runner.DaemonRunner):

    def __init__(self, *args, **kwd):
        super().__init__(*args, **kwd)

    # define the function that execute your stuff...
    def _mycommand(self):
        print('execute my command')

    # tell to the class how to reach that function
    action_funcs = runner.DaemonRunner.action_funcs
    action_funcs['mycommand'] = _mycommand


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()
# bind to your "MyRunner" instead of the generic one
daemon_runner = MyRunner(app)
daemon_runner.do_action()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39581046

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档