Python Apscheduler cron job from loop doesn't do all different versions -
i've got function gets , stores exchange every minute. run functions using (normally excellent) apscheduler. unfortunately, when add cron jobs loop, doesn't seem work expect to.
i've got little list couple strings, want run getandstore function. so:
from apscheduler.scheduler import scheduler apsched = scheduler() apsched.start() apsched.add_cron_job(lambda: getandstore('a'), minute='0-59') apsched.add_cron_job(lambda: getandstore('b'), minute='0-59') apsched.add_cron_job(lambda: getandstore('c'), minute='0-59')
this works fine, since i'm programmer , love automate stuff, this:
from apscheduler.scheduler import scheduler def getandstore(apicall): # call api using apicall value # , stores in db. print apicall apicalls = ['a', 'b', 'c'] apsched = scheduler() apsched.start() apicall in apicalls: print 'start cron for: ', apicall apsched.add_cron_job(lambda: getandstore(apicall), minute='0-59')
when run this, output following:
start cron for: start cron for: b start cron for: c c c c
the strange thing appears start a, b , c, starts cron c 3 times. bug in apscheduler? or doing wrong here?
all tips welcome!
this annoyed me while until figured out. so, created stackoverflow account after years of lurking. first post!
try dropping lambda (i know..., went down route too) , pass arguments via args tuple. i've used different scheduler below, should adapted.
from apscheduler.schedulers.background import backgroundscheduler import time def getandstore(apicall): # call api using apicall value # , stores in db. print(apicall) apicalls = ['a', 'b', 'c'] apsched = backgroundscheduler() apsched.start() apicall in apicalls: print ('start cron for: ' + apicall) apsched.add_job(getandstore, args=(apicall,), trigger='interval', seconds=1) # test while true: time.sleep(2)
output is:
start cron for: start cron for: b start cron for: c b c
Comments
Post a Comment