Download task_015/solution/code/pycron/cron.py from Lego-X/UXInteract: direct link, hf CLI and curl.
- Browser
- Download file 1.42 kB
-
https://huggingface.co/datasets/Lego-X/UXInteract/resolve/main/task_015/solution/code/pycron/cron.py
- Command line
-
hf download hf://datasets/Lego-X/UXInteract/task_015/solution/code/pycron/cron.py
-
curl -L -o cron.py https://huggingface.co/datasets/Lego-X/UXInteract/resolve/main/task_015/solution/code/pycron/cron.py
1.42 kB
| import threading | |
| import math | |
| class IntervalSchedule: | |
| def __init__(self, seconds): | |
| self.seconds = float(seconds) | |
| if not math.isfinite(self.seconds) or self.seconds <= 0: | |
| raise ValueError("interval seconds must be a positive finite number") | |
| class CronScheduler: | |
| def __init__(self): | |
| self.jobs = [] | |
| self._stop = threading.Event() | |
| self._threads = [] | |
| self._running = False | |
| def add_interval_job(self, seconds, func): | |
| if not callable(func): | |
| raise TypeError("interval callback must be callable") | |
| if self._running: | |
| raise RuntimeError("register jobs before starting the scheduler") | |
| self.jobs.append((IntervalSchedule(seconds), func)) | |
| def _run_job(self, schedule, func): | |
| while not self._stop.wait(schedule.seconds): | |
| func() | |
| def start(self): | |
| if self._running: | |
| return | |
| self._stop = threading.Event() | |
| self._threads = [] | |
| for schedule, func in self.jobs: | |
| thread = threading.Thread(target=self._run_job, args=(schedule, func), daemon=True) | |
| thread.start() | |
| self._threads.append(thread) | |
| self._running = True | |
| def stop(self): | |
| if not self._running: | |
| return | |
| self._stop.set() | |
| for thread in self._threads: | |
| thread.join(timeout=1) | |
| self._running = False | |