TypeError:第一个参数必须是可调用的 [英] TypeError: the first argument must be callable

查看:251
本文介绍了TypeError:第一个参数必须是可调用的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python并计划lib来创建类似cron的作业

I am using python and schedule lib to create a cron-like job

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)

在主体中调用时

cg = MyClass()
cg.sched_local(script_path, cron_definition)

我知道了:

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

在类中调用另一个方法而不是sched_local时,例如

When calling another method within the class instead of sched_local, like

def job(self):
    print "I am working"

工作正常.

推荐答案

do期望可调用对象以及它接受的所有参数.

do expects a callable and any arguments it make take.

因此,您对do的呼叫应类似于:

Therefore your call to do should look like:

schedule.every(1).minutes.do(self.local, script_path)

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

这篇关于TypeError:第一个参数必须是可调用的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆