Replace ad-hoc scheduler with package schedule
This commit is contained in:
parent
d5c57b35eb
commit
36749669b4
|
|
@ -56,7 +56,7 @@ $ sudo apt install python3-iso8601 python3-zeep libsdl2-ttf-2.0-0 python3-numpy
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
$ sudo apt install python3-pip
|
$ sudo apt install python3-pip
|
||||||
$ sudo pip3 install pygame gtfs_kit
|
$ sudo pip3 install pygame gtfs_kit schedule --break-system-packages
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,6 @@ import requests
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
import threading
|
import threading
|
||||||
import traceback
|
|
||||||
import zipfile
|
import zipfile
|
||||||
|
|
||||||
class GTFSClient():
|
class GTFSClient():
|
||||||
|
|
@ -46,7 +45,6 @@ class GTFSClient():
|
||||||
self._update_queue = update_queue
|
self._update_queue = update_queue
|
||||||
if update_interval_seconds and update_queue:
|
if update_interval_seconds and update_queue:
|
||||||
self._update_interval_seconds = update_interval_seconds
|
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:
|
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
|
return joined_data
|
||||||
|
|
||||||
|
|
||||||
def start(self) -> None:
|
|
||||||
""" Start the refresh thread """
|
|
||||||
self._refresh_thread.start()
|
|
||||||
self.refresh()
|
|
||||||
|
|
||||||
|
|
||||||
def refresh(self):
|
def refresh(self):
|
||||||
"""
|
"""
|
||||||
Create and enqueue the refreshed stop data
|
Create and enqueue the refreshed stop data
|
||||||
"""
|
"""
|
||||||
|
print("Refresh")
|
||||||
# Retrieve the GTFS-R deltas
|
# Retrieve the GTFS-R deltas
|
||||||
deltas, canceled_trips, added_stops = self.__poll_gtfsr_deltas()
|
deltas, canceled_trips, added_stops = self.__poll_gtfsr_deltas()
|
||||||
if len(deltas) > 0 or len(canceled_trips) > 0 or len(added_stops) > 0:
|
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)
|
self._update_queue.put(arrivals)
|
||||||
|
|
||||||
gc.collect()
|
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
|
|
||||||
|
|
|
||||||
4
main.py
4
main.py
|
|
@ -7,6 +7,7 @@ import gc
|
||||||
from glob import glob
|
from glob import glob
|
||||||
import pygame
|
import pygame
|
||||||
from pygame.locals import *
|
from pygame.locals import *
|
||||||
|
import schedule
|
||||||
from time import sleep
|
from time import sleep
|
||||||
import queue
|
import queue
|
||||||
from arrival_times import ArrivalTime
|
from arrival_times import ArrivalTime
|
||||||
|
|
@ -142,7 +143,7 @@ def main():
|
||||||
update_queue=update_queue,
|
update_queue=update_queue,
|
||||||
update_interval_seconds=config.update_interval_seconds)
|
update_interval_seconds=config.update_interval_seconds)
|
||||||
|
|
||||||
scheduler.start()
|
schedule.every(config.update_interval_seconds).seconds.do(scheduler.refresh)
|
||||||
|
|
||||||
# Main event loop
|
# Main event loop
|
||||||
running = True
|
running = True
|
||||||
|
|
@ -159,6 +160,7 @@ def main():
|
||||||
# Pygame event handling ends
|
# Pygame event handling ends
|
||||||
|
|
||||||
# Display update begins
|
# Display update begins
|
||||||
|
schedule.run_pending()
|
||||||
if update_queue.qsize() > 0:
|
if update_queue.qsize() > 0:
|
||||||
clear_screen()
|
clear_screen()
|
||||||
updates = update_queue.get()
|
updates = update_queue.get()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue