import logging import threading import time DEBUG = False try: from config import APP_NAME as ROOT_LOGGER_NAME except ImportError: ROOT_LOGGER_NAME = 'root' logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__) class background_task(object): RUN_SLEEP_TIME = 2.0 def __init__(self): self.__active__ = True self.__thread__ = threading.Thread(target=self.run, args=()) self.__thread__.daemon = True # Daemonize thread self.__thread__.start() # Start the execution def __run__(self): pass def run(self): while self.__active__: self.__run__() time.sleep(self.RUN_SLEEP_TIME) def close(self): self.__active__ = False self.__thread__.join() def __del__(self): self.close()