我在使用时遇到问题
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:计划程序已启动
DEBUG:apscheduler.scheduler:寻找要运行的作业
调试:apscheduler.scheduler:没有工作;等到添加工作
INFO:apscheduler.scheduler:将作业“ job_function(触发器:interval [0:00:30],下次运行于:2011-10-06 18:30:39)”添加到作业存储“默认”中
INFO:apscheduler.scheduler:将作业“ job_function(触发器:interval [0:00:30],下次运行于:2011-10-06 18:30:33)”添加到作业存储“默认”
DEBUG:apscheduler.scheduler:寻找要运行的作业
调试: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
最佳答案
我对python-daemon不太了解,但是未定义test_log
中的job_function()
。在引用init_schedule()
的Schedule
中,也会发生相同的问题。
关于python - python crontab替代-APScheduler和python-daemon,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7677441/