Search code examples
python-2.7apscheduler

why apscheduler use get_jobs empty?


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


Solution

  • 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()