Background thread's main function, execute registered tasks accordingly to their frequencies

def _run(cls):
        """Background thread's main function, execute registered tasks accordingly to their frequencies"""
        if cls._thread:
            with cls._lock:
                # First run: execute each task once to get it started
                for task in cls.tasks:
                    cls._execute_task(task)
                cls.tasks.sort()
                cls._last_execution = time.time()

        while cls._thread:
            with cls._lock:
                if cls.tasks:
                    for task in cls.tasks:
                        if task.next_execution - cls._last_execution < 0.5:
                            cls._execute_task(task)
                        else:
                            break
                    cls.tasks.sort()
                    cls._last_execution = time.time()
                    cls._sleep_delay = cls.tasks[0].next_execution - cls._last_execution

                else:
                    cls._sleep_delay = 1

                sleep_delay = max(0.1, cls._sleep_delay)

            # Don't hold cls._lock while sleeping, sleep delay should be 1 second when no tasks are present
            time.sleep(sleep_delay)