Pergunta

I'm trying to schedule a job every X hours within a class. However I'm not sure how to pass the current context to the method, since it requires "self". I know that if do it cron-style, i can use an args argument list, but that hasn't worked either. Help?

class MyClass(object):
    @settings.scheduler.interval_schedule(hours=2)
    def post(self, first_argument=None):
        # do stuff
        self.cleanup()

Results in

Traceback (most recent call last):
      File "/usr/local/lib/python2.7/dist-packages/apscheduler/scheduler.py", line 510, in _run_job
        retval = job.func(*job.args, **job.kwargs)
    TypeError: post() takes at least 1 argument (0 given)

Thanks.

Foi útil?

Solução

You can go this way:

class MyClass(object):
    def post(self, first_argument=None):
        # do stuff
        self.cleanup()


@settings.scheduler.interval_schedule(hours=2)
def my_job(first_argument=None):
    my_class = MyClass()
    my_class.post(first_argument)

Or, this way:

my_class = MyClass()
scheduler.add_job(my_class.post, 'interval', {'seconds': 3}, kwargs={'first_argument': first_argument})
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top