python - Scheduler going idle under multithreading -
i'm running python 3.3.2, win7/pro, 64-bit , have code in i'm trying run scheduler in own thread. appears when scheduler empties working queue, goes idle , not resume when new entry added queue. it's not obvious me docs supposed resume, not expect user code (running in thread outside control of scheduler) have worry possibility of scheduler has "finished". suspect there i'm not understanding and/or i'm misusing somehow, don't know where.
in example below, solved problem replacing scheduler's enter method own checks queue length , if zero, enters request , calls scheduler's run method again. seem work, feels wrong. there i'm missing?
class a(threading.thread): def __init__(self): super().__init__() self.schedcontrol = sched.scheduler(time.time, time.sleep) self.senter = self.schedcontrol.enter self.schedcontrol.enter = self.enter def print_time(self, a=''): """print current time , optional message.""" tmx = time.time() tms = time.strftime("%a, %d %b %y %h:%m:%s", time.localtime(tmx)) print("{0}: {1}".format(tms, a)) def run(self): #for call thread's start method self.schedcontrol.run() def enter(self, *args, **kwargs): tmp = false if len([x x in self.schedcontrol.queue]) == 0: tmp = true self.senter(*args, **kwargs) if tmp: print("restart") self.schedcontrol.run() class b(object): def __init__(self): self.scheduler = a() self.itemcount = 0 self.scheduler.schedcontrol.enter(1, 1, self.scheduler.print_time, argument=('starting scheduler',)) self.scheduler.start() def newitem(self): self.scheduler.schedcontrol.enter(1, 1, self.scheduler.print_time, argument=('new item: {0}'.format(self.itemcount),)) self.itemcount += 1 foo = b() foo.newitem() time.sleep(5) foo.newitem() time.sleep(5) foo.newitem() time.sleep(5) foo.newitem() time.sleep(5) foo.newitem() print("going sleep") time.sleep(100) sys.exit()
according docs, sched
run scheduled events run()
. text little obscure, believe run()
returns once scheduled events complete. if case need add more events , call run()
again, , given reported results seems happening.
Comments
Post a Comment