【问题标题】:Any reason why pid is not created and process does not start?没有创建 pid 并且进程没有启动的任何原因?
【发布时间】:2014-04-09 21:42:06
【问题描述】:

我正在尝试将此脚本与 python-daemon 一起使用,以在后台启动和停止某些代码的运行。 (最终目标是在 AWS 实例中使用它)。

由于某些让我无法理解的原因,没有生成 pid 文件,我认为该进程没有运行。

Python 脚本:

#standard python libs
import logging
import time

#third party libs
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 = '/Users/NAME/Documents/workspace/RandomThings/testdaemon.pid'
        self.pidfile_timeout = 5

    def run(self):
        while True:
            #Note that logger level needs to be set to logging.DEBUG before this shows up in the logs
            logger.debug("Debug message")
            logger.info("Info message")
            logger.warn("Warning message")
            logger.error("Error message")
            #Main code goes here ...
            sst=myClass()
            sst.run()
            time.sleep(10)

app = App()
logger = logging.getLogger("DaemonLog")
logger.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
handler = logging.FileHandler("/Users/NAME/Documents/workspace/RandomThings/testdaemon.log")
handler.setFormatter(formatter)
logger.addHandler(handler)

daemon_runner = runner.DaemonRunner(app)
#This ensures that the logger file handle does not get closed during daemonization
daemon_runner.daemon_context.files_preserve=[handler.stream]
daemon_runner.do_action()

我在 shell (OSX) 中做的:

python daemon.py start >>>> 这会运行,但没有创建 pid 文件(也尝试过 sudo) python daemon.py stop >>>> 返回错误:

Traceback(最近一次调用最后一次):文件“SST_daemon.py”,第 68 行,在 daemon_runner.do_action() 文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/daemon/runner.py”, 第 189 行,在 do_action func(自我)文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/daemon/runner.py”, 第 152 行,在 _stop u"PID 文件 %(pidfile_path)r 未锁定"% vars()) daemon.runner.DaemonRunnerStopFailureError: PID 文件 '/Users/josefernandes/Documents/workspace/RandomThings/testdaemon.pid' 未锁定

我已经尝试修复这个问题几个小时,但到目前为止没有结果。

这不起作用的任何原因?

非常感谢任何帮助!!!!!!

【问题讨论】:

    标签: python macos daemon pid python-daemon


    【解决方案1】:

    不确定为什么它对您不起作用,但也许下面的代码可能会解决一些问题。我确实必须即兴创作,因为我没有你提到的 myClass 。我的 Spam 类中有一个 run() 方法,它循环:是你的吗?如果这样做,您可能会惊讶于 App.run() 中的循环不会循环,因为它保留在 myClass 中。但这是一个问题。

    因为我没有 OSX,所以我不得不进行一些路径更改。但我尽量不改变任何其他东西,下面的代码对我有用:

      1 #standard python libs
      2 import logging
      3 import time
      4 
      5 #third party libs
      6 from daemon import runner
      7 import spam.eggs
      8 
      9 class App():
     10 
     11     def __init__(self):
     12         self.stdin_path = '/dev/null'
     13         self.stdout_path = '/dev/tty'
     14         self.stderr_path = '/dev/tty'
     15         self.pidfile_path = '/home/fbicknel/tmp/testdaemon.pid'
     16         self.pidfile_timeout = 5
     17 
     18     def run(self):
     19         while True:
     20             #Note that logger level needs to be set to logging.DEBUG before this shows up in the logs
     21             logger.debug("Debug message")
     22             logger.info("Info message")
     23             logger.warn("Warning message")
     24             logger.error("Error message")
     25             #Main code goes here ...
     26             # sst=myClass()
     27             # sst.run()
     28             sst=spam.eggs.Eggs()
     29             sst.run()
     30             time.sleep(10)
     31 
     32 app = App()
     33 logger = logging.getLogger("DaemonLog")
     34 logger.setLevel(logging.INFO)
     35 formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
     36 handler = logging.FileHandler("/home/fbicknel/tmp/testdaemon.log")
     37 handler.setFormatter(formatter)
     38 logger.addHandler(handler)
     39 
     40 daemon_runner = runner.DaemonRunner(app)
     41 #This ensures that the logger file handle does not get closed during daemonization
     42 daemon_runner.daemon_context.files_preserve=[handler.stream]
     43 daemon_runner.do_action()
    

    我尝试开始的一件事是完全消除 myClass 并让 run() 中的循环进行循环。那行得通,所以我添加了 spam.Eggs。

    希望这会让你再次前进。

    下面是我在 spam/eggs.py 中的模块:

      1 import time
      2 import logging
      3 
      4 class Eggs(object):
      5     def __init__(self, startvalue='green'):
      6         " init a spam object "
      7         self.logger = logging.getLogger("DaemonLog")
      8         self.color  = startvalue
      9 
     10     @property
     11     def color(self):
     12         return self._color
     13         
     14     @color.setter
     15     def color(self, value): 
     16         self._color = value
     17     
     18     def run(self):
     19         ' Just loop sounding happy. '
     20         while True:
     21             self.logger.info("yippie kai ai o")
     22             time.sleep(20)
     23             
     24 if __name__ == "__main__":
     25     spamalot = Eggs()
     26     print spamalot.color
     27     spamalot.color = 42
     28     print spamalot.color
    

    【讨论】:

    • 谢谢。现在正在经历它......我刚刚尝试了一个类似的带有“Hello World”类型类的守护进程,它工作了,但这个继续让我望而却步。我担心这可能是一个麻木的问题......
    • 嘿。这不起作用,因为我在 numpy(SVD) 中使用了一个函数,该函数在放入单独的进程/worker 时显然会崩溃。我最终将项目移动到 AWS 中的 linux 机器上,没有任何问题。
    猜你喜欢
    • 1970-01-01
    • 2018-01-11
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 2017-01-03
    • 2012-11-13
    • 2011-01-02
    • 2013-08-08
    相关资源
    最近更新 更多