I'm trying to schedule a job every X days within a class. However I'm not sure how to pass the current context to the method, since it requires "self". Help?
class Show(object):
def __init__(self, scheduler): self.scheduler = scheduler
def test(self, para1, para2, para3): # do something
def add(self):
# do something
show = Show(self.scheduler) self.scheduler.add_job(show.test, 'date', args = [para1, para2, para3], run_date = datetime.datetime.now() + datetime.timedelta(seconds=10))
and I get response:
Job "Show.test (trigger: date[2017-04-25 10:05:07 CST], next run at: 2017-04-25 10:05:07 CST)"
Traceback (most recent call last): File "/usr/local/lib/python2.7/site-packages/apscheduler/executors/base.py", line 125, in run_job retval = job.func(*job.args, **job.kwargs) TypeError: unbound method test() must be called with Orders instance as first argument (got tuple instance instead)
# para1 is a tuple instance
I searched this issue with Google and find a similary issue in stackoverflow: http://stackoverflow.com/questions/18421078/passing-current-object-to-python-apscheduler-method
The only answer suggested:
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})
But this answer seems not work for me....(My example code have used this suggested method)
Help~~~ QAQ
self.scheduler.add_job(Show.test, 'date', args=[self, para1,
para2, para3], run_date=datetime.datetime.now() +
datetime.timedelta(seconds=10))
Scheduling bound methods does not work very well on old
Python versions. You may have better luck on Python 3.
--
You received this message because you are subscribed to the Google Groups "APScheduler" group.
To unsubscribe from this group and stop receiving emails from it, send an email to apscheduler...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
File "/usr/local/lib/python2.7/site-packages/apscheduler/schedulers/base.py", line 425, in add_job
job = Job(self, **job_kwargs)
File "/usr/local/lib/python2.7/site-packages/apscheduler/job.py", line 44, in __init__
self._modify(id=id or uuid4().hex, **kwargs)
File "/usr/local/lib/python2.7/site-packages/apscheduler/job.py", line 175, in _modify
check_callable_args(func, args, kwargs)
File "/usr/local/lib/python2.7/site-packages/apscheduler/util.py", line 379, in check_callable_args
'(allowed: %d, given in args: %d)' % (len(args) - len(unmatched_args), len(args)))
ValueError: The list of positional arguments is longer than the target callable can handle (allowed: 3, given in args: 4)
File "/usr/local/lib/python2.7/site-packages/apscheduler/schedulers/base.py", line 434, in add_job
self._real_add_job(job, jobstore, replace_existing)
File "/usr/local/lib/python2.7/site-packages/apscheduler/schedulers/base.py", line 851, in _real_add_job
store.add_job(job)
File "/usr/local/lib/python2.7/site-packages/apscheduler/jobstores/sqlalchemy.py", line 89, in add_job
'job_state': pickle.dumps(job.__getstate__(), self.pickle_protocol)
PicklingError: Can't pickle <type 'NoneType'>: attribute lookup __builtin__.NoneType failed
File "/usr/local/lib/python2.7/site-packages/apscheduler/schedulers/base.py", line 434, in add_job
self._real_add_job(job, jobstore, replace_existing)
File "/usr/local/lib/python2.7/site-packages/apscheduler/schedulers/base.py", line 851, in _real_add_job
store.add_job(job)
File "/usr/local/lib/python2.7/site-packages/apscheduler/jobstores/sqlalchemy.py", line 89, in add_job
'job_state': pickle.dumps(job.__getstate__(), self.pickle_protocol)
PicklingError: Can't pickle <type 'NoneType'>: attribute lookup __builtin__.NoneType failed
Try this: