dbader / schedule

Python job scheduling for humans.
https://schedule.readthedocs.io/
MIT License
11.73k stars 959 forks source link

Function with argument can't able to run schedule #518

Closed rudSarkar closed 2 years ago

rudSarkar commented 2 years ago

Hi,

I faced an issue when running a schedule function with an argument in the while loop, more specifically the loop worked well at first, and then when it tried to run again it crashed with a stack trace. Check the code below:

import time
import schedule

def main(ar):
    print(f"ok this is: {ar}")

if __name__ == "__main__":
    schedule.every(10).seconds.do(main("Test"))

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

Now when I run this script it returns an error

C:\Users\Raj\Desktop\pigeon-dev>python test2.py
ok this is: Test
Traceback (most recent call last):
  File "test2.py", line 10, in <module>
    schedule.every(10).seconds.do(main("Test"))
  File "C:\Users\Raj\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\schedule\__init__.py", line 625, in do
    self.job_func = functools.partial(job_func, *args, **kwargs)
TypeError: the first argument must be callable

The stack trace are indicate this line

https://github.com/dbader/schedule/blob/ed7d21961242f86342da240ee1d089c1cee37de6/schedule/__init__.py#L625

I approached a different way and figured out that the schedule did not work with argument, eg: if you run this script:

import time
import schedule

def main(ar):
    print(f"ok this is: {ar}")

if __name__ == "__main__":
    schedule.every(10).seconds.do(main("Test"))

This script returns the output because of the main("Test") function at the first run taking no delay if it has no argument it waits for 10s to run the main() function.

Wanted to know if there is support for working with the function argument?

Thanks, @rudSarkar

SijmenHuizenga commented 2 years ago

Hi, Thanks question with clear sample code! Yes, you can pass an argument to a job like so:

schedule.every(10).seconds.do(main, "Test")