[英]TypeError: the first argument must be callable
I am using python and schedule lib to create a cron-like job 我正在使用python和schedule lib来创建一个类似cron的工作
class MyClass:
def local(self, command):
#return subprocess.call(command, shell=True)
print "local"
def sched_local(self, script_path, cron_definition):
import schedule
import time
#job = self.local(script_path)
schedule.every(1).minutes.do(self.local(script_path))
while True:
schedule.run_pending()
time.sleep(1)
When calling this in a main 在主要时调用它
cg = MyClass()
cg.sched_local(script_path, cron_definition)
I got this: 我懂了:
local
Traceback (most recent call last):
File "MyClass.py", line 131, in <module>
cg.sched_local(script_path, cron_definition)
File "MyClass.py", line 71, in sched_local
schedule.every(1).minutes.do(self.local(script_path))
File "/usr/local/lib/python2.7/dist-packages/schedule/__init__.py", line 271, in do
self.job_func = functools.partial(job_func, *args, **kwargs)
TypeError: the first argument must be callable
When calling another method within the class instead of sched_local, like 在类中调用另一个方法而不是sched_local时,比如
def job(self):
print "I am working"
The job works fine. 工作顺利。
do
expects a callable and any arguments it make take. do
预计可调用的,它使任何参数。
Therefore your call to do
should look like: 因此您的来电
do
应该是这样的:
schedule.every(1).minutes.do(self.local, script_path)
The do
implementation can be found here . 可以在此处找到
do
实现。
def do(self, job_func, *args, **kwargs):
"""Specifies the job_func that should be called every time the
job runs.
Any additional arguments are passed on to job_func when
the job runs.
"""
self.job_func = functools.partial(job_func, *args, **kwargs)
functools.update_wrapper(self.job_func, job_func)
self._schedule_next_run()
return self
replace 更换
schedule.every(1).minutes.do(self.local(script_path))
with this one: 这一个:
schedule.every(1).minutes.do(self.local,script_path)
and it will work fine.. 它会工作正常..
you should write the parameters of function after function name and comma separate them.. 你应该在函数名和逗号分隔之后编写函数的参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.