'Process scheduled tasks.'

def _process(self):

    self.run = True
    try:
        while (self.run and (not self.stop.is_set())):
            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:
                    newtask = None
                    while (self.run and (not self.stop.is_set()) and (newtask is None) and (wait > 0)):
                        try:
                            newtask = self.addq.get(True, min(wait, self.wait_timeout))
                        except QueueEmpty:
                            wait -= self.wait_timeout
            except QueueEmpty:
                self.schedule_lock.acquire()
                relevant = itertools.takewhile((lambda task: (time.time() >= task.next)), self.schedule)
                status = map((lambda task: (task, task.run())), relevant)
                for (task, doRepeat) in status:
                    if (not doRepeat):
                        try:
                            self.schedule.remove(task)
                        except ValueError:
                            pass
                    else:
                        updated = True
            else:
                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))
                self.schedule_lock.release()
    except KeyboardInterrupt:
        self.run = False
    except SystemExit:
        self.run = False
    log.debug('Quitting    Scheduler    thread')
