为什么apscheduler 使用get_jobs 空? [英] why apscheduler use get_jobs empty?

查看:179
本文介绍了为什么apscheduler 使用get_jobs 空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的test.py

from datetime import datetime, timedelta导入系统导入操作系统从 apscheduler.schedulers.blocking 导入 BlockingScheduler从 apscheduler.jobstores.sqlalchemy 导入 SQLAlchemyJobStore从 apscheduler.jobstores.redis 导入 RedisJobStore工作商店 = {#'default': SQLAlchemyJobStore(url='sqlite:///jobs.sqlite')'默认':RedisJobStore(host='localhost', port=6379)}scheduler = BlockingScheduler(jobstores=jobstores)定义警报(时间):print('警报!此警报已安排在 %s.' % 时间)如果 __name__ == '__main__':alarm_time = datetime.now() + timedelta(seconds=10)scheduler.add_job(alarm, 'interval', seconds=10, args=[datetime.now()], name='alarm_test')print('要清除警报,请删除 example.sqlite 文件.')print('按 Ctrl+{0} 退出'.format('Break' if os.name == 'nt' else 'C'))尝试:调度程序.start()除了(键盘中断,系统退出):经过

我成功执行了 python test.py 运行作业

然后通过putty使用另一个终端

蟒蛇>>>导入redis>>>从测试导入 *>>>r = redis.Redis()>>>r.keys()>>>r.zrange('apscheduler.run_times',0,1)

它将找到作业 ID 57841c0ee05249efb466882265f2c495

<预><代码>>>>ret = scheduler.get_jobs(jobstore='default')

ret 为空为什么???

非常感谢

解决方案

在运行 get_jobs() 之前是否启动了调度程序?如果没有,它只会列出暂定的作业.这就是为什么你没有看到这份工作.

试试这个:

from apscheduler.schedulers.background import BackgroundScheduler从 apscheduler.jobstores.redis 导入 RedisJobStore调度程序 = BackgroundScheduler()scheduler.add_jobstore('redis', host='localhost', port=6379)scheduler.start(暂停=真)scheduler.print_jobs()

this is my test.py

from datetime import datetime, timedelta
import sys
import os

from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore
from apscheduler.jobstores.redis import RedisJobStore

jobstores = {
        #'default': SQLAlchemyJobStore(url='sqlite:///jobs.sqlite')
        'default': RedisJobStore(host='localhost', port=6379)
}
scheduler = BlockingScheduler(jobstores=jobstores)

def alarm(time):
    print('Alarm! This alarm was scheduled at %s.' % time)


if __name__ == '__main__':
    alarm_time = datetime.now() + timedelta(seconds=10)
    scheduler.add_job(alarm, 'interval', seconds=10, args=[datetime.now()], name='alarm_test')
    print('To clear the alarms, delete the example.sqlite file.')
    print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))

    try:
        scheduler.start()
    except (KeyboardInterrupt, SystemExit):
        pass

i do python test.py run job successfully

and then use another terminal by putty

python
>>> import redis
>>> from test import *
>>> r = redis.Redis()
>>> r.keys()
>>> r.zrange('apscheduler.run_times',0,1)

it will find the job id 57841c0ee05249efb466882265f2c495

>>> ret = scheduler.get_jobs(jobstore='default')

ret is empty why???

thanks a lot

解决方案

Have you started the scheduler before running get_jobs()? If not, it will only list tentatively scheduled jobs. That's why you're not seeing the job.

Try this instead:

from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.jobstores.redis import RedisJobStore

scheduler = BackgroundScheduler()
scheduler.add_jobstore('redis', host='localhost', port=6379)
scheduler.start(paused=True)
scheduler.print_jobs()

这篇关于为什么apscheduler 使用get_jobs 空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆