Using schedule module to reming me to drink water every ten seconds

Question:

I am using schedule module to remind me to drink water every ten seconds

import schedule


def remindDrink():
    print("Drink Water")
while True:
    schedule.every().day.at("16:35").do(remindDrink())

So the problem here is that the task gets executed, but immedieately, not at the given time, and VSCode throws a weird error at me

Traceback (most recent call last):
  File "e:CodePython CoderandomModule.py", line 12, in <module>
    schedule.every().day.at("16:31").do(sendNotification())
  File "C:UsersPCAppDataLocalProgramsPythonPython310libsite-packagesschedule__init__.py", line 625, in do
    self.job_func = functools.partial(job_func, *args, **kwargs)
TypeError: the first argument must be callable
PS E:CodePython Code> 

This is the error, what am I doing wrong?

Asked By: Illusioner_

||

Answers:

Remove the () from remindDrink() in the last line inside the do() function

Your code should look like this:

schedule.every().day.at("16:35").do(remindDrink)

Refer back to this question: TypeError: the first argument must be callable in scheduler library

Answered By: Faisal Abughazaleh

Same module different approach, I personally prefer this approach because it keeps my work clean, easy to read and to understand at your first glance and ofcourse easy to refactor.

from schedule import every, repeat, run_pending
import time

@repeat(every().day.at("16:35"))
def remindDrink():
    print("Drink Water")

while True:
    run_pending()
    time.sleep(1)

Your broken code fixed:
Your broken code is fixed below, now the choice is yours, you can either use the above code or this:

import schedule
import time

def remindDrink():
    print("Drink Water")

schedule.every().day.at("16:35").do(remindDrink)

while True:
    schedule.run_pending()
    time.sleep(1)
Answered By: Jamiu Shaibu

quick thought, shedule….do(), within do() you don’t run the function, just put the name of the function inside do.
”’
schedule.every().day.at("16:35").do(remindDrink)
”’

Answered By: grifway
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.