简体   繁体   English

TypeError:第一个参数必须是可调用的

[英]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.

相关问题 TypeError:第一个参数必须是可调用的 - TypeError: first argument must be callable 类型错误:第一个参数必须在调度程序库中可调用 - TypeError: the first argument must be callable in scheduler library TypeError:第一个参数必须是可调用的,defaultdict - TypeError: first argument must be callable, defaultdict 电报TypeError:第一个参数必须可调用 - Telegram TypeError: the first argument must be callable 调度库 TypeError:第一个参数必须是可调用的 - Schedule library TypeError: the first argument must be callable Pandas 造型为 excel “TypeError:第一个参数必须是可调用的” - Pandas styling to excel “TypeError: the first argument must be callable” 第一个参数必须是可调用的或无 - first argument must be callable or None 类型错误:第一个参数必须是可调用的或无 - 错误不是第一次出现而是稍后出现 - TypeError: first argument must be callable or None - error is not coming first time but coming later TypeError:当我在django的views.py文件中导入调度程序时,第一个参数必须是可调用的? - TypeError: the first argument must be callable when I import a scheduler in my views.py file of django? TypeError:deafultdict必须具有可调用的第一个参数 - TypeError: deafultdict must have first arguments callable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM