0

我在我的脚本中使用 python 2.7.3 和 daemon runner。在运行(循环)方法中,我想睡一段时间,但不是用这样的代码:

while True:
    time.sleep(10)

我想等待一些同步原语,例如 multiprocessing.Event。有我的代码:

# -*- coding: utf-8 -*-
import logging
from daemon import runner
import signal
import multiprocessing

import spyder_cfg

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', datefmt='%m-%d %H:%M', filename=spyder_cfg.log_file)

class Daemon(object):     
    def __init__(self, pidfile_path):
        self.stdin_path = '/dev/null'
        self.stdout_path = '/dev/tty'
        self.stderr_path = '/dev/tty'
        self.pidfile_path = None
        self.pidfile_timeout = 5
        self.pidfile_path = pidfile_path 

    def setup_daemon_context(self, daemon_context):
        self.daemon_context = daemon_context

    def run(self):
        logging.info('Spyder service has started')
        logging.debug('event from the run() = {}'.format(self.daemon_context.stop_event))
        while not self.daemon_context.stop_event.wait(10):
            try:
                logging.info('Spyder is working...')
            except BaseException as exc:
                logging.exception(exc)
        logging.info('Spyder service has been stopped') 

    def handle_exit(self, signum, frame):
        try:
            logging.info('Spyder stopping...')
            self.daemon_context.stop_event.set()
        except BaseException as exc:
            logging.exception(exc)

if __name__ == '__main__':
    app = Daemon(spyder_cfg.pid_file)
    d = runner.DaemonRunner(app)
    d.daemon_context.working_directory = spyder_cfg.work_dir
    d.daemon_context.files_preserve = [h.stream for h in logging.root.handlers]
    d.daemon_context.signal_map = {signal.SIGUSR1: app.handle_exit} 
    d.daemon_context.stop_event = multiprocessing.Event()
    app.setup_daemon_context(d.daemon_context)
    logging.debug('event from the main = {}'.format(d.daemon_context.stop_event))
    d.do_action()

这是我的日志文件记录:

06-04 11:32 root         DEBUG    event from the main = <multiprocessing.synchronize.Event object at 0x7f0ef0930d50>
06-04 11:32 root         INFO     Spyder service has started
06-04 11:32 root         DEBUG    event from the run() = <multiprocessing.synchronize.Event object at 0x7f0ef0930d50>
06-04 11:32 root         INFO     Spyder is working...
06-04 11:32 root         INFO     Spyder stopping...

日志中没有“Spyder 服务已停止”打印,我的程序挂在 set() 调用上。在调试时,我看到它在 Event.set() 调用时挂起,set 方法在所有等待实体唤醒时挂在信号量上。没有理由 Event 将是全局对象或 threading.Event。我看到了这个答案,但这对我不利。是否有与 multiprocessing.Event 相同的行为等待超时等待的替代方法?

4

1 回答 1

0

我确实从信号处理程序打印堆栈,我认为存在死锁,因为信号处理程序与我的主进程使用相同的堆栈,当我调用 Event.set() 时,堆栈上的方法 wait() 更高......

def handle_exit(self, signum, frame):
    try:
        logging.debug('Signal handler:{}'.format(traceback.print_stack()))
    except BaseException as exc:
        logging.exception(exc)


d.do_action()
  File ".../venv/work/local/lib/python2.7/site-packages/daemon/runner.py", line 189, in do_action
    func(self)
  File ".../venv/work/local/lib/python2.7/site-packages/daemon/runner.py", line 134, in _start
    self.app.run()
  File ".../venv/work/skelet/src/spyder.py", line 32, in run
    while not self.daemon_context.stop_event.wait(10):
  File "/usr/lib/python2.7/multiprocessing/synchronize.py", line 337, in wait
    self._cond.wait(timeout)
  File "/usr/lib/python2.7/multiprocessing/synchronize.py", line 246, in wait
    self._wait_semaphore.acquire(True, timeout)
  File ".../venv/work/skelet/src/spyder.py", line 41, in handle_exit
    logging.debug('Signal handler:{}'.format(traceback.print_stack()))

这就是此修复解决问题的原因:

def handle_exit(self, signum, frame):  
    t = Timer(1, self.handle_exit2)  
    t.start()

def handle_exit2(self): 
    self.daemon_context.stop_event.set()
于 2014-06-04T12:17:33.130 回答