def run(spin=SPIN): _log.debug("run spin=%r", spin) global running, taskManager, deferredFns, sleeptime # reference the task manager (a singleton) taskManager = TaskManager() # count how many times we are going through the loop loopCount = 0 running = True while running: # _log.debug("time: %r", time.time()) loopCount += 1 # get the next task task, delta = taskManager.get_next_task() try: # if there is a task to process, do it if task: # _log.debug("task: %r", task) taskManager.process_task(task) # if delta is None, there are no tasks, default to spinning if delta is None: delta = spin # there may be threads around, sleep for a bit if sleeptime and (delta > sleeptime): time.sleep(sleeptime) delta -= sleeptime # if there are deferred functions, use a small delta if deferredFns: delta = min(delta, 0.001) # _log.debug("delta: %r", delta) # loop for socket activity asyncore.loop(timeout=delta, count=1) # check for deferred functions while deferredFns: # get a reference to the list fnlist = deferredFns deferredFns = [] # call the functions for fn, args, kwargs in fnlist: # _log.debug("call: %r %r %r", fn, args, kwargs) fn( *args, **kwargs) # done with this list del fnlist except KeyboardInterrupt: _log.info("keyboard interrupt") running = False except Exception, e: _log.exception("an error has occurred: %s", e)
def run_once(): """ Make a pass through the scheduled tasks and deferred functions just like the run() function but without the asyncore call (so there is no socket IO actviity) and the timers. """ _log.debug("run_once") global taskManager, deferredFns # reference the task manager (a singleton) taskManager = TaskManager() try: delta = 0.0 while delta == 0.0: # get the next task task, delta = taskManager.get_next_task() _log.debug(" - task, delta: %r, %r", task, delta) # if there is a task to process, do it if task: taskManager.process_task(task) # check for deferred functions while deferredFns: # get a reference to the list fnlist = deferredFns deferredFns = [] # call the functions for fn, args, kwargs in fnlist: _log.debug(" - call: %r %r %r", fn, args, kwargs) fn( *args, **kwargs) # done with this list del fnlist except KeyboardInterrupt: _log.info("keyboard interrupt") except Exception, e: _log.exception("an error has occurred: %s", e)