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

时间:2014-10-27 08:29:22

标签: python methods schedule

我正在使用python并安排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)

在主要

中调用此内容时
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,比如

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

工作顺利。

2 个答案:

答案 0 :(得分:11)

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

因此,您对do的调用应如下所示:

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

可以找到do实施{。{3}}。

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

答案 1 :(得分:4)

替换

<DataGrid CanUserAddRows="True"
          AutoGenerateColumns="False"
          IsReadOnly="False"
          ItemsSource="{Binding Collection}">
    <DataGrid.Columns>
        <DataGridTextColumn Width="*" 
                            Header="Value" 
                            Binding="{Binding Text}"/>
    </DataGrid.Columns>
</DataGrid>

与此:

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

它将正常工作..

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