-2

The python project I'm working on, is a webpage that outputs covid data and news at a scheduled time. The project must use the sched module.

The issue I'm having it to do with schedule_covid_update() function. When calling the function. You have 2 required argument, the duration/time for the update to occur, the name of the update. And the 2 optional arguments, news and data. Which are used to tell schedule_covid_update() if both or just one update type is occurring at the scheduled time.

Now, if I were to call the function twice like this for example:

schedule_covid_update(4, "test update 2", news="news", data="data")
schedule_covid_update(0, "test update 1", news="news", data="data")

I would expect the data for "test update 1" to occur and return the data before "test update 2", regardless of the order I called schedule_covid_update(). But It doesn't, it schedules "test update 2".

Also when testing this code. You can just try some other function instead of updateData and updateNews. As I can't give the code for the functions.

scheduler.py:

import time, sched
from datetime import datetime
from covid_data_handler import *


s = sched.scheduler(time.time, time.sleep)
def schedule_covid_update (update_interval, update_name, news="", data=""):
    print("update name: " + update_name)

    if(news=="news" and data=="data"):
        #s.enter(timeDuration(update_interval),1, updateData)
        #s.enter(timeDuration(update_interval),1, updateNews)
        s.enter(update_interval,1, updateData)
        s.enter(update_interval,1, updateNews)
        print("Updating news and data")
    elif(news=="news" and data!="data"):
        #s.enter(timeDuration(update_interval),1, updateNews)
        s.enter(update_interval,1, updateNews)
        print("Updating news only")
    elif(news!="news" and data=="data"):
        #s.enter(timeDuration(update_interval),1, updateData)
        s.enter(update_interval,1, updateData)
        print("Updating data only")
        #scheduler.run(blocking=False)
    else:
        print("No Update")

    s.run()


schedule_covid_update(4, "test update 2", news="news", data="data")
schedule_covid_update(0, "test update 1", news="news", data="data")
martineau
  • 119,623
  • 25
  • 170
  • 301
  • Please see: [Why is Can someone help me? not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) for more details. – itprorh66 Jul 19 '22 at 15:13
  • Also see [Under what circumstances may I add "urgent" or other similar phrases to my question, in order to obtain faster answers?](https://meta.stackoverflow.com/questions/326569/under-what-circumstances-may-i-add-urgent-or-other-similar-phrases-to-my-quest) – martineau Jul 19 '22 at 20:02

1 Answers1

1

Take a look at the docs for scheduler.run . With s.run() inside of your function, it blocks forever and never reaches the second scheduled task. Schedule first, then run the scheduler independently, outside of the scheduling functions.

Dominik Stańczak
  • 2,046
  • 15
  • 27