【问题标题】:python crontab alternative - APScheduler & python-daemonpython crontab 替代方案 - APScheduler & python-daemon
【发布时间】:2011-10-06 16:25:16
【问题描述】:

我在使用时遇到问题 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 信息:apscheduler.scheduler:调度程序已启动 DEBUG:apscheduler.scheduler:寻找要运行的作业 调试:apscheduler.scheduler:没有工作;等到添加作业 INFO:apscheduler.scheduler:将作业“job_function(触发器:间隔[0:00:30],下次运行时间:2011-10-06 18:30:39)”添加到作业存储“默认” INFO:apscheduler.scheduler:将作业“job_function(触发器:间隔[0:00:30],下次运行时间:2011-10-06 18:30:33)”添加到作业存储“默认” DEBUG:apscheduler.scheduler:寻找要运行的作业 DEBUG:apscheduler.scheduler:下次唤醒时间为 2011-10-06 18:30:33 (in 10.441128 seconds)

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

编辑 - 工作

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

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

【问题讨论】:

  • 我在 Python 中有一个简单的 cron 替换。它可用here 和入口点脚本是here 如果你想试一试。我还没有时间将它重构为一个单独的应用程序。
  • 谢谢努法尔。我试试看。
  • 欢迎您的反馈。我真的应该把它作为一个单独的包拿出来。

标签: python scheduler python-daemon


【解决方案1】:

我对python-daemon了解不多,但是job_function()中的test_log没有定义。同样的问题出现在您引用Scheduleinit_schedule() 中。

【讨论】:

  • rocksportsrocker,test_log 是在 main 中定义的,我之前试过运行它,Python 解释器没有报错。我在 daemon.DaemonContext 中导入调度程序,在我看来这是正确的做法,因为当守护程序分叉时,任何打开的套接字或文件都被强制关闭。
  • 不幸的是,没有。我添加了一个示例输出。在第二种情况下,输出为空白且未写入 test.log。目标是生成一个守护进程 pid 并让任务无限期地运行。
  • 你能在 with-context 中引入调试日志吗?只是为了看看什么被执行,什么不被执行......
  • 我已经在 DaemonContext 中添加了调试日志和 stdout,stderr 并最终让它工作。(在问题中添加了 EDIT)。感谢您的耐心等待!
  • 忘记将 'sched' 对象传递给 schedule_job 调用,在编辑之前我有: schedule_job(job_function, 120, '2011-10-06 12:30:09') 这通常会得到被 try/except 构造捕获,但 DaemonContext 掩盖了它,隐藏了所有输出。
猜你喜欢
  • 1970-01-01
  • 2021-10-07
  • 1970-01-01
  • 1970-01-01
  • 2021-04-13
  • 2011-02-09
  • 2015-06-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多