Python - lock.aquire()上的python-daemon lockfile超时

时间:2014-01-10 20:12:53

标签: python file locking daemon

我正在使用python-daemon模块来管理我的Python脚本的守护进程。 但是,我在运行脚本时遇到了一个令人头疼的问题,我根本无法弄清楚。我也不知道如何开始调试它。

我有代码:

def run_application():
    #Do something here...

class App():
    def __init__(self):
        self.stdin_path = '/dev/null'
        self.stdout_path = 'stdout.txt'
        self.stderr_path = 'stdlog.log'
        self.pidfile_path = 'filelock.pid'
        self.pidfile_timeout = 5
    def run(self):
        run_application()


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

运行时,它总是将以下内容写入stdlog.log

Traceback (most recent call last):
  File "MyApp.py", line 335, in <module>
    daemon_runner.do_action()
  File "/anaconda/lib/python2.7/site-packages/daemon/runner.py", line 189, in do_action
    func(self)
  File "/anaconda/lib/python2.7/site-packages/daemon/runner.py", line 124, in _start
    self.daemon_context.open()
  File "/anaconda/lib/python2.7/site-packages/daemon/daemon.py", line 346, in open
    self.pidfile.__enter__()
  File "/anaconda/lib/python2.7/site-packages/lockfile/__init__.py", line 229, in __enter__
    self.acquire()
  File "/anaconda/lib/python2.7/site-packages/daemon/pidfile.py", line 42, in acquire
    super(TimeoutPIDLockFile, self).acquire(timeout, *args, **kwargs)
  File "/anaconda/lib/python2.7/site-packages/lockfile/pidlockfile.py", line 88, in acquire
    self.path)
lockfile.LockTimeout: Timeout waiting to acquire lock for /MyApp/filelock.pid

因此在尝试锁定filelock.pid时似乎超时。我不知道为什么会这样。我已删除filelock.pid,我已更改权限;每次都有同样的错误。

我该如何开始调试?我很茫然。

我正在使用python-daemon version 1.6(如果重要的话)。

更新

根据建议here,我现在看到已经有一个进程正在运行。现在我该如何确定如何确定正在运行的守护进程的PID。

2 个答案:

答案 0 :(得分:2)

我同意@ExploWare,就他如何证明你可以捕获那些LockTimeout例外而言。

因此,就调试方式和查看此锁定的进程而言,这是一个可以运行的外部代码...

import daemon.pidfile
import os
import lockfile

# We know the lockfile name.
pidfile = daemon.pidfile.PIDLockFile(
                            os.path.join("/MyApp/","filelock.pid"))

# This current process id...
os.getpid()
# 46337

那么,如果有什么进程获得此锁定?

pidfile.is_locked()
# True

pidfile.read_pid()
# 96856

当我们的PIDLockFile实例尝试&#34; 获取&#34;,

pidfile.__dict__
# {'unique_name': '/MyApp/filelock.pid', 'lock_file': '/MyApp/filelock.pid.lock', 'hostname': 
# 'MyMachine.local', 'pid': 46337, 'timeout': None, 'tname': '', 'path': '/MyApp/filelock.pid'}

pidfile.acquire()
#
# (Had to Control-C quit because I didnt set a timeout on PIDLockFile )
#
# ^CTraceback (most recent call last):
#   File "<stdin>", line 1, in <module>
#   File "/Users/michal/venf/lib/python2.7/site-packages/lockfile/pidlockfile.py", line 92, in acquire
#     time.sleep(timeout is not None and timeout/10 or 0.1)
# KeyboardInterrupt

相反,请使用@ExploWare的异常捕获。

# Wait only 5 seconds.
pidfile.timeout = 5

try:
    pidfile.acquire()
except lockfile.LockTimeout:
    print 'locked . need to wait or move on.'
# 
# locked . need to wait or move on.

答案 1 :(得分:0)

我找到了处理此异常的好方法,所以也许它对你有帮助:
添加
    from lockfile import LockTimeout
到脚本的开头并像这样环绕daemon_runner.doaction()

try:
    daemon_runner.do_action()
except LockTimeout:
    print "Error: couldn't aquire lock"
    #you can exit here or try something else