gpt4 book ai didi

python - 类型错误 : the first argument must be callable in scheduler library

转载 作者:行者123 更新时间:2023-12-01 06:28:28 26 4
gpt4 key购买 nike

运行调度程序库时出现以下错误。我确实经历过类似的问题和答案,但不知何故无法识别导致错误的根源。有人可以帮我解决我出错的地方吗?

TypeError: the first argument must be callable

import schedule
import time


class Scheduler():

def trigger_testsuite(self):
print("I am working as expected.")

def scheule_a_job(self, type="Secs", interval=5):

if (type == "Mins"):
schedule.every(interval).minutes.do(self.trigger_testsuite())

if (type == "Secs"):
schedule.every(interval).seconds.do(self.trigger_testsuite())

if (interval == "Hrs"):
schedule.every().hour.do(self.trigger_testsuite())

if (interval == "Daily"):
schedule.every().day.at("10:00").do(self.trigger_testsuite())

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


if __name__ == "__main__":
run = Scheduler()
run.scheule_a_job()

回溯:-

I am working as expected.
Traceback (most recent call last):
File "foo/Scheduler.py", line 31, in <module>
run.scheule_a_job()
File "foo/Scheduler.py", line 16, in scheule_a_job
schedule.every(interval).seconds.do(self.trigger_testsuite())
File "foo\Python\Python38-32\lib\site-packages\schedule\__init__.py", line 440, in do
self.job_func = functools.partial(job_func, *args, **kwargs)
TypeError: the first argument must be callable

Process finished with exit code 1

最佳答案

不要在传递给的函数中包含括号:

schedule.every(interval).minutes.do()

所以,这一行:

schedule.every(interval).minutes.do(self.trigger_testsuite())

应该是:

schedule.every(interval).minutes.do(self.trigger_testsuite)

其他人也一样。最终代码变为:

import schedule
import time


class Scheduler():

def trigger_testsuite(self):
print("I am working as expected.")

def scheule_a_job(self, type="Secs", interval=5):

if (type == "Mins"):
schedule.every(interval).minutes.do(self.trigger_testsuite)

if (type == "Secs"):
schedule.every(interval).seconds.do(self.trigger_testsuite)

if (interval == "Hrs"):
schedule.every().hour.do(self.trigger_testsuite)

if (interval == "Daily"):
schedule.every().day.at("10:00").do(self.trigger_testsuite)

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


if __name__ == "__main__":
run = Scheduler()
run.scheule_a_job()

关于python - 类型错误 : the first argument must be callable in scheduler library,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60018745/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com