From 36749669b451c1e007bed80f5bddee49e3efa996 Mon Sep 17 00:00:00 2001 From: Nahuel Lofeudo Date: Sun, 17 Sep 2023 17:47:08 +0100 Subject: [PATCH] Replace ad-hoc scheduler with package schedule --- README.md | 2 +- gtfs_client.py | 27 +-------------------------- main.py | 4 +++- 3 files changed, 5 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index b49d087..be74cbe 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ $ sudo apt install python3-iso8601 python3-zeep libsdl2-ttf-2.0-0 python3-numpy ```shell $ sudo apt install python3-pip -$ sudo pip3 install pygame gtfs_kit +$ sudo pip3 install pygame gtfs_kit schedule --break-system-packages ``` diff --git a/gtfs_client.py b/gtfs_client.py index 2eedb63..29af4a8 100644 --- a/gtfs_client.py +++ b/gtfs_client.py @@ -11,7 +11,6 @@ import requests import sys import time import threading -import traceback import zipfile class GTFSClient(): @@ -46,7 +45,6 @@ class GTFSClient(): self._update_queue = update_queue if update_interval_seconds and update_queue: self._update_interval_seconds = update_interval_seconds - self._refresh_thread = threading.Thread(target=lambda: every(update_interval_seconds, self.refresh)) def _read_feed(self, path: gk.Path, dist_units: str, stop_codes: list[str]) -> gk.Feed: """ @@ -346,16 +344,11 @@ class GTFSClient(): return joined_data - def start(self) -> None: - """ Start the refresh thread """ - self._refresh_thread.start() - self.refresh() - - def refresh(self): """ Create and enqueue the refreshed stop data """ + print("Refresh") # Retrieve the GTFS-R deltas deltas, canceled_trips, added_stops = self.__poll_gtfsr_deltas() if len(deltas) > 0 or len(canceled_trips) > 0 or len(added_stops) > 0: @@ -394,21 +387,3 @@ class GTFSClient(): self._update_queue.put(arrivals) gc.collect() - return arrivals - - -def every(delay, task) -> None: - """ Auxilliary function to schedule updates. - Taken from https://stackoverflow.com/questions/474528/what-is-the-best-way-to-repeatedly-execute-a-function-every-x-seconds - """ - next_time = time.time() + delay - while True: - time.sleep(max(0, next_time - time.time())) - try: - task() - except Exception: - traceback.print_exc() - # in production code you might want to have this instead of course: - # logger.exception("Problem while executing repetitive task.") - # skip tasks if we are behind schedule: - next_time += (time.time() - next_time) // delay * delay + delay diff --git a/main.py b/main.py index 94dd537..87c24ba 100755 --- a/main.py +++ b/main.py @@ -7,6 +7,7 @@ import gc from glob import glob import pygame from pygame.locals import * +import schedule from time import sleep import queue from arrival_times import ArrivalTime @@ -142,7 +143,7 @@ def main(): update_queue=update_queue, update_interval_seconds=config.update_interval_seconds) - scheduler.start() + schedule.every(config.update_interval_seconds).seconds.do(scheduler.refresh) # Main event loop running = True @@ -159,6 +160,7 @@ def main(): # Pygame event handling ends # Display update begins + schedule.run_pending() if update_queue.qsize() > 0: clear_screen() updates = update_queue.get()