[Need Help] How can i passing current object to python apscheduler method?

1,740 views
Skip to first unread message

晨小巫

unread,
Apr 24, 2017, 10:16:28 PM4/24/17
to APScheduler

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?


Here is example code:
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


Alex Grönholm

unread,
Apr 25, 2017, 7:47:03 AM4/25/17
to apsch...@googlegroups.com

Try this:

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.

晨小巫

unread,
Apr 26, 2017, 2:50:45 AM4/26/17
to APScheduler
I try this:

self.scheduler.add_job(show.test, 'date', args=[self, para1, para2, para3], run_date=datetime.datetime.now() + datetime.timedelta(seconds=10))

and this:

self.scheduler.add_job(show.test, 'date', args=[show, para1, para2, para3], run_date=datetime.datetime.now() + datetime.timedelta(seconds=10))

Now it‘s an exception in add_job:

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)

if i do like this:
self.scheduler.add_job(Show.test, 'date', args=[self, para1, para2, para3], run_date=datetime.datetime.now() + datetime.timedelta(seconds=10))

I get this:
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




在 2017年4月25日星期二 UTC+8下午7:47:03,Alex Grönholm写道:

晨小巫

unread,
Apr 26, 2017, 5:23:56 AM4/26/17
to APScheduler
Thanks. This suggestion is useful.

I found the exception(below) is because the object is not alive when wakeup
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


在 2017年4月25日星期二 UTC+8下午7:47:03,Alex Grönholm写道:

Try this:

晨小巫

unread,
Apr 27, 2017, 1:53:59 AM4/27/17
to APScheduler
QAQ 

I am wrong....This exception in when add_job(),the object must have been alive at the time.

I plan to change the job function from class fuction to global function to avoid these problems = =

在 2017年4月26日星期三 UTC+8下午5:23:56,晨小巫写道:

unlike dis

unread,
Sep 29, 2017, 10:53:51 PM9/29/17
to APScheduler


在 2017年4月27日星期四 UTC+8下午1:53:59,晨小巫写道:
我说怎么这段英文看起来那么好理解,一看发帖人,中文名.......

 

melo...@gmail.com

unread,
Dec 5, 2017, 1:56:04 AM12/5/17
to APScheduler
你的job store 应该不是 RAMJobStore 如果是这个问题就不会发生了


在 2017年4月27日星期四 UTC+8下午1:53:59,晨小巫写道:
QAQ 
Reply all
Reply to author
Forward
0 new messages