use threading.Timer instead of the sched module
This commit is contained in:
parent
ffe22cf819
commit
1795d16112
2 changed files with 27 additions and 16 deletions
|
|
@ -18,34 +18,45 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import sched
|
from threading import Timer, Event
|
||||||
import time
|
|
||||||
|
|
||||||
|
|
||||||
__all__ = ['Scheduler']
|
__all__ = ['Scheduler']
|
||||||
|
|
||||||
|
class IScheduler(object):
|
||||||
class Scheduler(object):
|
|
||||||
def __init__(self):
|
|
||||||
self.scheduler = sched.scheduler(time.time, time.sleep)
|
|
||||||
self.running = False
|
|
||||||
|
|
||||||
def schedule(self, interval, function, *args):
|
def schedule(self, interval, function, *args):
|
||||||
return self.scheduler.enter(interval, 1, function, args)
|
raise NotImplementedError()
|
||||||
|
|
||||||
def repeat(self, interval, function, *args):
|
def repeat(self, interval, function, *args):
|
||||||
return self.scheduler.enter(interval, 1, self._repeated_cb, (interval, function, args))
|
raise NotImplementedError()
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
self.running = True
|
raise NotImplementedError()
|
||||||
while self.running:
|
|
||||||
self.scheduler.run()
|
def want_stop(self):
|
||||||
if not self.scheduler.queue:
|
raise NotImplementedError()
|
||||||
self.scheduler.delayfunc(0.001)
|
|
||||||
|
class Scheduler(IScheduler):
|
||||||
|
def __init__(self):
|
||||||
|
self.stop_event = Event()
|
||||||
|
self.count = 0
|
||||||
|
self.queue = {}
|
||||||
|
|
||||||
|
def schedule(self, interval, function, *args):
|
||||||
|
self.count += 1
|
||||||
|
timer = Timer(interval, function, args)
|
||||||
|
timer.start()
|
||||||
|
self.queue[self.count] = timer
|
||||||
|
return self.count
|
||||||
|
|
||||||
|
def repeat(self, interval, function, *args):
|
||||||
|
return self.schedule(interval, self._repeated_cb, interval, function, args)
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
self.stop_event.wait()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def want_stop(self):
|
def want_stop(self):
|
||||||
self.running = False
|
self.stop_event.set()
|
||||||
|
|
||||||
def _repeated_cb(self, interval, func, args):
|
def _repeated_cb(self, interval, func, args):
|
||||||
func(*args)
|
func(*args)
|
||||||
|
|
|
||||||
|
|
@ -23,13 +23,13 @@ from PyQt4.QtCore import QTimer, SIGNAL
|
||||||
from PyQt4.QtGui import QMainWindow, QApplication
|
from PyQt4.QtGui import QMainWindow, QApplication
|
||||||
|
|
||||||
from weboob import Weboob
|
from weboob import Weboob
|
||||||
from weboob.scheduler import Scheduler
|
from weboob.scheduler import IScheduler
|
||||||
|
|
||||||
from .base import BaseApplication
|
from .base import BaseApplication
|
||||||
|
|
||||||
__all__ = ['QtApplication']
|
__all__ = ['QtApplication']
|
||||||
|
|
||||||
class QtScheduler(Scheduler):
|
class QtScheduler(IScheduler):
|
||||||
def __init__(self, app):
|
def __init__(self, app):
|
||||||
self.app = app
|
self.app = app
|
||||||
self.timers = {}
|
self.timers = {}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue