brad999
brad999

Reputation: 33

How to run concurrent AP Scheduler jobs?

I'm attempting to run multiple AP Scheduler jobs in my program (both interval and cron), but when I add multiple interval jobs with different intervals they all execute at the shortest interval. For example, if I add one job with a frequency of 30 seconds and one with 15 seconds, both will execute every 15 seconds.

My code is below. How do I properly run these two jobs on separate intervals?

from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.executors.pool import ProcessPoolExecutor

executors = {
             'default': {'type': 'threadpool', 'max_workers': 20},
             'processpool': ProcessPoolExecutor(max_workers=5)
            }
job_defaults = {
                'coalesce': False,
                'max_instances': 3
               }
sched = BackgroundScheduler(executors=executors, job_defaults=job_defaults, timezone="EST")

sched.start()
sched.add_job(lambda: module.handle(self.profile, mic), 'interval', id=module.__name__, seconds=15)
sched.add_job(lambda: module2.handle(self.profile, mic), 'interval', id=module2.__name__, seconds=30)
atexit.register(lambda: sched.shutdown(wait=False))

Upvotes: 2

Views: 3869

Answers (2)

brad999
brad999

Reputation: 33

This turned out to be an issue with the lambda that I was using. I removed the lambda by adding the args parameter like below.

sched.add_job(module.handle, 'interval', args=[self.profile, mic], id=module.__name__, seconds=15)
sched.add_job(module2.handle, 'interval', args=[self.profile, mic], id=module.__name__, seconds=30)

Upvotes: 0

Alexey Smirnov
Alexey Smirnov

Reputation: 2613

You add 2 jobs

sched.add_job(lambda: module.handle(self.profile, mic), 'interval', id=module.__name__, seconds=15)
sched.add_job(lambda: module2.handle(self.profile, mic), 'interval', id=module.__name__, seconds=30)

with dublicate ID's of id=module.__name__. Do you need to specify ID's by yourself? Apscheduler can do it for you.

Upvotes: 0

Related Questions