TypeError: the first argument must be callable

4m1nh4j1 picture 4m1nh4j1 · Oct 27, 2014 · Viewed 19.9k times · Source

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)

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

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

The job works fine.

Answer

Dunes picture Dunes · Oct 27, 2014

do expects a callable and any arguments it make take.

Therefore your call to do should look like:

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

The do implementation can be found here.

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