我想在课堂上每隔X小时安排一份工作.但是我不确定如何将当前上下文传递给方法,因为它需要“自我”.我知道如果做cron风格,我可以使用args参数列表,但这也没有用.救命?
class MyClass(object):
@settings.scheduler.interval_schedule(hours=2)
def post(self, first_argument=None):
# do stuff
self.cleanup()
结果是
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)
谢谢.
解决方法:
你可以这样:
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)
或者,这样:
my_class = MyClass()
scheduler.add_job(my_class.post, 'interval', {'seconds': 3}, kwargs={'first_argument': first_argument})