From 38dc9c005cfc102166d61f70f8764186ca72212b Mon Sep 17 00:00:00 2001 From: April Date: Sun, 13 Nov 2022 14:29:46 -0700 Subject: [PATCH] Make each record update its own task --- RecordTasks.py | 22 +++++++++++++++++ main.py | 65 +++++++++----------------------------------------- 2 files changed, 33 insertions(+), 54 deletions(-) create mode 100644 RecordTasks.py diff --git a/RecordTasks.py b/RecordTasks.py new file mode 100644 index 0000000..4d7be17 --- /dev/null +++ b/RecordTasks.py @@ -0,0 +1,22 @@ +import asyncio +from recordGenerators import CurrentObservations,HourlyForecast,DailyForecast + + +""" This houses the tasks needed to update the data records concurrently + I have no idea if this is a messy way to do things, but it will be worked upon if it is. +""" + +async def coTask(): + while True: + await CurrentObservations.makeDataFile() + asyncio.sleep(5 * 60) + +async def hfTask(): + while True: + await HourlyForecast.makeDataFile() + asyncio.sleep(60 * 60) + +async def dfTask(): + while True: + await DailyForecast.makeDataFile() + asyncio.sleep(60 * 60) \ No newline at end of file diff --git a/main.py b/main.py index 4268849..80781d8 100644 --- a/main.py +++ b/main.py @@ -2,10 +2,10 @@ import asyncio from asyncio.log import logger from asyncore import loop import logging,coloredlogs -from recordGenerators import Alerts,CurrentObservations,DailyForecast,HourlyForecast,AirportDelays,AirQuality,HeatingAndCooling,PollenForecast,Breathing, AchesAndPains, MosquitoActivity, WateringNeeds, TideForecast from radar import TWCRadarCollector import os from datetime import datetime +import RecordTasks l = logging.getLogger(__name__) coloredlogs.install(logger=l) @@ -36,58 +36,15 @@ Alerts: 5 minutes l.info("Starting i2RecordCollector") l.info("Developed by mewtek32, Floppaa, Goldblaze, and needlenose") -async def grabAlertsLoop(): - while True: - await Alerts.makeRecord() - await asyncio.sleep(60) +async def main(): + coTask = asyncio.create_task(RecordTasks.coTask()) + hfTask = asyncio.create_task(RecordTasks.hfTask()) + dfTask = asyncio.create_task(RecordTasks.dfTask()) -async def FiveMinUpdaters(): - while True: - await CurrentObservations.makeDataFile() - l.debug("Sleeping for 5 minutes...") - await asyncio.sleep(5 * 60) + # In theory, these should all run concurrently without problems + await coTask + await hfTask + await dfTask -async def HourUpdaters(): - while True: - await DailyForecast.makeDataFile() - await HourlyForecast.makeDataFile() - # AirQuality.writeData() - # PollenForecast.makeDataFile() - # AirportDelays.writeData() - # Breathing.makeDataFile() - # HeatingAndCooling.makeRecord() - # WateringNeeds.makeRecord() - # MosquitoActivity.makeRecord() - # AchesAndPains.makeRecord() - # TideForecast.makeRecord() - l.debug("Sleeping for an hour...") - await asyncio.sleep(60 * 60) - -async def radarCollector(): - mosaicUpdateIntervals = [0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55] - satradUpdateIntervals = [0, 10, 20, 30, 40, 50] - - while True: - # Server takes ~15 - 35 seconds on average to fully generate a frame, use 40 seconds - # to make sure the radar frame is fully good to go - if datetime.now().minute in mosaicUpdateIntervals and datetime.now().second == 40: - await TWCRadarCollector.collect("radarmosaic") - - if datetime.now().minute in satradUpdateIntervals and datetime.now().second == 45: - await TWCRadarCollector.collect("satrad") - - await asyncio.sleep(1) - -loop = asyncio.get_event_loop() -alertTask = loop.create_task(grabAlertsLoop()) -CCtask = loop.create_task(FiveMinUpdaters()) -ForecastsTask = loop.create_task(HourUpdaters()) - -if useRadarServer: radarTask = loop.create_task(radarCollector()) - -try: - loop.run_until_complete(alertTask) - loop.run_until_complete(CCtask) - loop.run_until_complete(ForecastsTask) - if useRadarServer: loop.run_until_complete(radarTask) -except asyncio.CancelledError: pass \ No newline at end of file +if __name__ == "__main__": + asyncio.run(main()) \ No newline at end of file