1

I am new using python and I have a python code that have 3 functions in it. i want to run these three function after every X number of seconds. I am using the APScheduler package and add_job method. I see a warning message saying "skipped: maximum number of running instances reached (1)" while i implement for one function. So, what will happen when i schedule all the three functions using add_job method?

Code looks something like this

  scheduler1 = APScheduler()
scheduler2 = APScheduler()
scheduler3 = APScheduler()

def fun1():
  print("From Func1")

def fun2():
  print("From Func2")

def fun3():
  print("From Func3")


if __name__ == '__main__':
    scheduler1.add_job(id='Scheduled task', func=fun1, trigger='interval', seconds=5)
    scheduler.start()
    scheduler2.add_job(id='Scheduled task', func=fun2, trigger='interval', seconds=5)
    scheduler.start()
    scheduler3.add_job(id='Scheduled task', func=fun3, trigger='interval', seconds=5)
    scheduler.start()

Using the above type of code, Can I achieve a scheduler that is running three jobs in the background and all of them run every 5 seconds?

bbb
  • 149
  • 2
  • 18
  • Giving all of the jobs the same id looks incorrect. – timgeb Sep 20 '21 at 07:23
  • ok, what about the warning message "skipped: maximum number of running instances reached (1)". Is this fine? and i want to know if this is the right method to schedule a function to run for every 5seconds – bbb Sep 20 '21 at 07:32
  • 1
    You have three schedulers. You only need one. Create one scheduler, add each job to that scheduler and then start it using `scheduler.start()`. No need to run start each time you add a job, just do it at the end. – scotty3785 Sep 20 '21 at 07:34
  • 1
    Also at the moment, your program will just end when the three tasks have been started. Add a `while True` loop at the end with `time.sleep(1)` inside it to keep the code running. – scotty3785 Sep 20 '21 at 07:35
  • As an English tweak, "run for" means duration (you want the jobs to stop after "every few seconds") whereas simply "run" would seem to make more sense for what you seem to be asking (or "run at" but then you need to shuffle the rest of the sentence quite a bit), i.e. you want three new jobs to be run "every few seconds". – tripleee Sep 20 '21 at 07:35
  • Does this answer your question? [python apscheduler - skipped: maximum number of running instances reached](https://stackoverflow.com/questions/34020161/python-apscheduler-skipped-maximum-number-of-running-instances-reached) – CrazyChucky Sep 20 '21 at 07:49
  • import apscheduler.schedulers.blocking scheduler = apscheduler.schedulers.blocking.BackgroundScheduler('apscheduler.job_defaults.max_instances':'2') ^ SyntaxError: invalid syntax – bbb Sep 20 '21 at 07:57
  • 1
    @George See my comment on that answer, and/or look at the other answers. – CrazyChucky Sep 20 '21 at 08:17
  • @CrazyChucky I dont see the warning message after i use BacgroundScheduler – bbb Sep 20 '21 at 08:24

2 Answers2

3

First of all there is nothing wrong with your code just the silly typing mistakes with object names and its obviously not optimized.

Following is the your version by fixing the typos

from apscheduler.schedulers.background import BackgroundScheduler
from time import sleep

scheduler1 = BackgroundScheduler()
scheduler2 = BackgroundScheduler()
scheduler3 = BackgroundScheduler()

def fun1():
  print("From Func1")

def fun2():
  print("From Func2")

def fun3():
  print("From Func3")


if __name__ == '__main__':
    scheduler1.add_job(id='Scheduled task', func=fun1, trigger='interval', seconds=5)
    scheduler1.start()
    scheduler2.add_job(id='Scheduled task', func=fun2, trigger='interval', seconds=5)
    scheduler2.start()
    scheduler3.add_job(id='Scheduled task', func=fun3, trigger='interval', seconds=5)
    scheduler3.start()
    while True:
        sleep(1)

i added extra sleep function to stop the program from being killed and test whether or not timer is working, its working just fine

following version is the optimized form of your code

from apscheduler.schedulers.background import BackgroundScheduler
from time import sleep

scheduler = BackgroundScheduler()

def fun1():
  print("From Func1")

def fun2():
  print("From Func2")

def fun3():
  print("From Func3")


if __name__ == '__main__':
    scheduler.add_job(id='Scheduled task 1', func=fun1, trigger='interval', seconds=5)
    scheduler.add_job(id='Scheduled task 2', func=fun2, trigger='interval', seconds=5)
    scheduler.add_job(id='Scheduled task 3', func=fun3, trigger='interval', seconds=5)
    scheduler.start()
    while True:
        sleep(1)

using the single scheduler object to run all functions after specified period of time.

enter image description here

Zain Ul Abidin
  • 2,467
  • 1
  • 17
  • 29
  • 1
    This program will just end after 20 seconds and the tasks will just stop running. You need a while loop to keep the program running. – scotty3785 Sep 20 '21 at 07:37
  • thanks Zain, what do you think about this warning message "skipped: maximum number of running instances reached (1)"? – bbb Sep 20 '21 at 07:38
  • 1
    looks like your hardware is not supporting this number of instances as i didn't get such warning my specs are Intel Core i5, 8th gen, 16GB RAM, python 3.9 – Zain Ul Abidin Sep 20 '21 at 07:41
  • let me attach screenshot – Zain Ul Abidin Sep 20 '21 at 07:41
  • And can I rely on this code to run continuously for a couple of days(with 5 sec interval)? – bbb Sep 20 '21 at 07:42
  • have not benchedmark looks like you have to attach some events with your scheduler so that it can check whether its been killed by OS then restart it – Zain Ul Abidin Sep 20 '21 at 07:43
  • for those who are downvoting i just corrected his code not develoepd a full tool to make entire software for him – Zain Ul Abidin Sep 20 '21 at 07:51
  • read the question carefully, his code had an error and we need to fix that not develop the whole scenario – Zain Ul Abidin Sep 20 '21 at 07:53
  • I expect most of them were from the lack of a while loop to keep the program running, but you've now fixed that. – CrazyChucky Sep 20 '21 at 08:02
  • @ZainUlAbidin Do you know how can i trigger all these jobs at once? instead of sequential calling one after the other ? – bbb Sep 20 '21 at 09:29
  • 1
    Ask this as a separate question, you will have to store the reference of your all functions in the list to iterate and execute all at once – Zain Ul Abidin Sep 20 '21 at 10:11
-1

Does this work?

import time

scheduler = APScheduler()

def fun1():
  print("From Func1")

def fun2():
  print("From Func2")

def fun3():
  print("From Func3")


if __name__ == '__main__':
    while True:
        scheduler.add_job(id='Scheduled task', func=fun1, trigger='interval', seconds=5)
        scheduler.add_job(id='Scheduled task', func=fun2, trigger='interval', seconds=5)
        scheduler.add_job(id='Scheduled task', func=fun3, trigger='interval', seconds=5)
        scheduler.start()
        time.sleep(20)

It should run forever until you press Ctrl+c

You can also try it like this

import time

scheduler = APScheduler()

def fun1():
  print("From Func1")

def fun2():
  print("From Func2")

def fun3():
  print("From Func3")

scheduler.add_job(id='Scheduled task', func=fun1, trigger='interval', seconds=5)
scheduler.add_job(id='Scheduled task', func=fun2, trigger='interval', seconds=5)
scheduler.add_job(id='Scheduled task', func=fun3, trigger='interval', seconds=5)
scheduler.start()

if __name__ == '__main__':
    while True:
        time.sleep(20)

This way you only add the jobs to the scheduler once and you run the start loop over and over again.

anarchy
  • 3,709
  • 2
  • 16
  • 48