diff options
author | Lance Stout <lancestout@gmail.com> | 2013-06-22 14:35:20 -0700 |
---|---|---|
committer | Lance Stout <lancestout@gmail.com> | 2013-06-22 14:35:20 -0700 |
commit | e76a48393159592e73421541ed05e78c287faf00 (patch) | |
tree | 4875c5a9d3e2df57f79b8e3800470ce315a8c1e7 /sleekxmpp/xmlstream/scheduler.py | |
parent | c0437d2de86f5b6ffce17061fc3aed0e35441fd3 (diff) | |
parent | 37a804320204726480732704036fad9c7f6fff78 (diff) | |
download | slixmpp-e76a48393159592e73421541ed05e78c287faf00.tar.gz slixmpp-e76a48393159592e73421541ed05e78c287faf00.tar.bz2 slixmpp-e76a48393159592e73421541ed05e78c287faf00.tar.xz slixmpp-e76a48393159592e73421541ed05e78c287faf00.zip |
Merge branch 'develop' of github.com:fritzy/SleekXMPP into develop
Diffstat (limited to 'sleekxmpp/xmlstream/scheduler.py')
-rw-r--r-- | sleekxmpp/xmlstream/scheduler.py | 32 |
1 files changed, 21 insertions, 11 deletions
diff --git a/sleekxmpp/xmlstream/scheduler.py b/sleekxmpp/xmlstream/scheduler.py index bef8f5e5..e6fae37a 100644 --- a/sleekxmpp/xmlstream/scheduler.py +++ b/sleekxmpp/xmlstream/scheduler.py @@ -20,6 +20,11 @@ import itertools from sleekxmpp.util import Queue, QueueEmpty +#: The time in seconds to wait for events from the event queue, and also the +#: time between checks for the process stop signal. +WAIT_TIMEOUT = 1.0 + + log = logging.getLogger(__name__) @@ -120,6 +125,10 @@ class Scheduler(object): #: Lock for accessing the task queue. self.schedule_lock = threading.RLock() + #: The time in seconds to wait for events from the event queue, + #: and also the time between checks for the process stop signal. + self.wait_timeout = WAIT_TIMEOUT + def process(self, threaded=True, daemon=False): """Begin accepting and processing scheduled tasks. @@ -139,24 +148,25 @@ class Scheduler(object): self.run = True try: while self.run and not self.stop.is_set(): - wait = 0.1 updated = False if self.schedule: wait = self.schedule[0].next - time.time() + else: + wait = self.wait_timeout try: if wait <= 0.0: newtask = self.addq.get(False) else: - if wait >= 3.0: - wait = 3.0 newtask = None - elapsed = 0 - while not self.stop.is_set() and \ + while self.run and \ + not self.stop.is_set() and \ newtask is None and \ - elapsed < wait: - newtask = self.addq.get(True, 0.1) - elapsed += 0.1 - except QueueEmpty: + wait > 0: + try: + newtask = self.addq.get(True, min(wait, self.wait_timeout)) + except QueueEmpty: # Nothing to add, nothing to do. Check run flags and continue waiting. + wait -= self.wait_timeout + except QueueEmpty: # Time to run some tasks, and no new tasks to add. self.schedule_lock.acquire() # select only those tasks which are to be executed now relevant = itertools.takewhile( @@ -174,11 +184,11 @@ class Scheduler(object): # only need to resort tasks if a repeated task has # been kept in the list. updated = True - else: - updated = True + else: # Add new task self.schedule_lock.acquire() if newtask is not None: self.schedule.append(newtask) + updated = True finally: if updated: self.schedule.sort(key=lambda task: task.next) |