1

我在使用 python-daemon 1.6和 APScheduler 来管理任务列表时遇到问题。

(调度程序需要在特定选择的时间定期运行它们 - 秒分辨率)

工作(直到按下 Ctrl+C),

from apscheduler.scheduler import Scheduler
import logging
import signal

def job_function():
    print "Hello World"

def init_schedule():
        logging.basicConfig(level=logging.DEBUG)
        sched = Scheduler()
        # Start the scheduler
        sched.start()

        return sched

def schedule_job(sched, function, periodicity, start_time):
        sched.add_interval_job(job_function, seconds=periodicity, start_date=start_time)

if __name__ == "__main__":

    sched = init_schedule()
    schedule_job(sched, job_function, 120, '2011-10-06 12:30:09')
    schedule_job(sched, job_function, 120, '2011-10-06 12:31:03')

    # APSScheduler.Scheduler only works until the main thread exits
    signal.pause()
    # Or
    #time.sleep(300)

样本输出:

INFO:apscheduler.threadpool:已启动线程池,核心线程数为 0,最大线程数为 20 等到添加作业 INFO:apscheduler.scheduler:已将作业“job_function(触发器:interval [0:00:30],下次运行时间:2011-10-06 18:30:39)”添加到作业存储“默认”信息:apscheduler.scheduler:将作业“job_function(触发器:interval[0:00:30],下次运行时间:2011-10-06 18:30:33)”添加到作业存储“默认”调试:apscheduler.scheduler:寻找要运行的作业 DEBUG:apscheduler.scheduler:下一次唤醒到期时间为 2011-10-06 18:30:33(在 10.441128 秒内)

使用 python-daemon, 输出为空白。为什么 DaemonContext 不能正确生成进程?

编辑 - 工作

在阅读了 python-daemon 源代码后,我将 stdout 和 stderr 添加到了 DaemonContext 中,终于能够知道发生了什么。

def job_function():
    print "Hello World"
    print >> test_log, "Hello World"

def init_schedule():
    logging.basicConfig(level=logging.DEBUG)
    sched = Scheduler()
    sched.start()

    return sched

def schedule_job(sched, function, periodicity, start_time):
    sched.add_interval_job(job_function, seconds=periodicity, start_date=start_time)

if __name__ == "__main__":

    test_log = open('daemon.log', 'w')
    daemon.DaemonContext.files_preserve = [test_log]

    try:
           with daemon.DaemonContext():
                   from datetime import datetime
                   from apscheduler.scheduler import Scheduler
                   import signal

                   logging.basicConfig(level=logging.DEBUG)
                   sched = init_schedule()

                   schedule_job(sched, job_function, 120, '2011-10-06 12:30:09')
                   schedule_job(sched, job_function, 120, '2011-10-06 12:31:03')

                   signal.pause()

    except Exception, e:
           print e
4

1 回答 1

1

我对python-daemon了解不多,但test_log没有job_function()定义。同样的问题出现在init_schedule()你引用的地方Schedule

于 2011-10-06T16:54:25.990 回答