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

4m1*_*4j1 7 python methods schedule

我正在使用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)
Run Code Online (Sandbox Code Playgroud)

在主要时调用它

cg = MyClass()
cg.sched_local(script_path, cron_definition)
Run Code Online (Sandbox Code Playgroud)

我懂了:

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
Run Code Online (Sandbox Code Playgroud)

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

def job(self):
    print "I am working"
Run Code Online (Sandbox Code Playgroud)

工作顺利.

Dun*_*nes 13

do 期望一个可调用的和它所做的任何参数.

因此,您的电话do应该如下:

schedule.every(1).minutes.do(self.local, script_path)
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)


Zei*_*sab 6

更换

schedule.every(1).minutes.do(self.local(script_path))
Run Code Online (Sandbox Code Playgroud)

与此:

schedule.every(1).minutes.do(self.local,script_path)
Run Code Online (Sandbox Code Playgroud)

它将正常工作..

您应该在函数名称和逗号分隔后写出函数参数。