From 262915adfbebc5bbd04941b5510f26b521fe6409 Mon Sep 17 00:00:00 2001 From: SpudGunMan Date: Tue, 30 Jul 2024 10:59:58 -0700 Subject: [PATCH] better asyncio loops --- config.template | 2 +- mesh_bot.py | 21 +++++++++++++++------ pong_bot.py | 49 +++++++++++++++++++++++++++++-------------------- 3 files changed, 45 insertions(+), 27 deletions(-) diff --git a/config.template b/config.template index 4fd7595..2fb0d2f 100644 --- a/config.template +++ b/config.template @@ -19,7 +19,7 @@ type = serial port = /dev/ttyUSB0 #port = /dev/ttyACM1 # port = COM1 -# hostname = 192.168.0.1 +# hostname = meshtastic.local # mac = 00:11:22:33:44:55 [general] diff --git a/mesh_bot.py b/mesh_bot.py index ae5da26..0e78768 100755 --- a/mesh_bot.py +++ b/mesh_bot.py @@ -4,6 +4,7 @@ import asyncio # for the event loop import time # for sleep, get some when you can :) +from signal import SIGINT, SIGTERM from pubsub import pub # pip install pubsub from modules.settings import * from modules.system import * @@ -298,7 +299,7 @@ def onReceive(packet, interface): print(packet) # print the packet for debugging print("END of packet \n") -def start_rx(): +async def start_rx(): print ("\nMeshtastic Autoresponder Bot CTL+C to exit\n") if bbs_enabled: print(f"System: BBS Enabled, using {bbsdb}") @@ -324,12 +325,16 @@ def start_rx(): f"{get_name_from_number(myNodeNum2, 'short', 2)}. NodeID: {myNodeNum2}, {decimal_to_hex(myNodeNum2)}") print (msg) while True: - time.sleep(0.5) # sleep to allow the event loop to process + await asyncio.sleep(0.5) + for signal in [SIGINT, SIGTERM]: + messageLoop.add_signal_handler(signal.SIGINT, rxLoop.cancel) + #exit_handler() pass def exit_handler(): # Close the interface and save the BBS messages print(f"\n{log_timestamp()} System: Closing Autoresponder\n") + rxLoop.cancel() interface1.close() print(f"{log_timestamp()} System: Interface1 Closed") if interface2_enabled: @@ -339,14 +344,18 @@ def exit_handler(): save_bbsdb() print(f"{log_timestamp()} System: BBS Messages Saved") print(f"{log_timestamp()} System: Exiting") + messageLoop.stop() + messageLoop.close() exit (0) # Hello World -loop = asyncio.new_event_loop() +messageLoop = asyncio.new_event_loop() +rxLoop = asyncio.ensure_future(start_rx(), loop=messageLoop) + try: - loop.run_forever(start_rx()) -finally: - loop.close() + messageLoop.run_forever() +except KeyboardInterrupt: exit_handler() + pass # EOF diff --git a/pong_bot.py b/pong_bot.py index 0211e66..6afc49f 100755 --- a/pong_bot.py +++ b/pong_bot.py @@ -4,6 +4,7 @@ import asyncio # for the event loop import time # for sleep, get some when you can :) +from signal import SIGINT, SIGTERM from pubsub import pub # pip install pubsub from modules.settings import * from modules.system import * @@ -188,19 +189,7 @@ def onReceive(packet, interface): print(packet) # print the packet for debugging print("END of packet \n") - -def exit_handler(): - # Close the interface and save the BBS messages - print(f"\n{log_timestamp()} System: Closing Autoresponder\n") - interface1.close() - print(f"{log_timestamp()} System: Interface1 Closed") - if interface2_enabled: - interface2.close() - print(f"{log_timestamp()} System: Interface2 Closed") - print(f"{log_timestamp()} System: Exiting") - exit (0) - -def start_rx(): +async def start_rx(): # Start the receive loop pub.subscribe(onReceive, 'meshtastic.receive') msg = (f"{log_timestamp()} System: Autoresponder Started for Device1 {get_name_from_number(myNodeNum, 'long', 1)}," @@ -211,17 +200,37 @@ def start_rx(): f"{get_name_from_number(myNodeNum2, 'short', 2)}. NodeID: {myNodeNum2}, {decimal_to_hex(myNodeNum2)}") print (msg) while True: - time.sleep(0.5) # sleep to allow the event loop to process + await asyncio.sleep(0.5) + for signal in [SIGINT, SIGTERM]: + messageLoop.add_signal_handler(signal.SIGINT, rxLoop.cancel) + #exit_handler() pass -# Hello World -print ("\nMeshtastic Autoresponder Pong Bot CTL+C to exit\n") +def exit_handler(): + # Close the interface and save the BBS messages + print(f"\n{log_timestamp()} System: Closing Autoresponder\n") + rxLoop.cancel() + interface1.close() + print(f"{log_timestamp()} System: Interface1 Closed") + if interface2_enabled: + interface2.close() + print(f"{log_timestamp()} System: Interface2 Closed") + if bbs_enabled: + save_bbsdb() + print(f"{log_timestamp()} System: BBS Messages Saved") + print(f"{log_timestamp()} System: Exiting") + messageLoop.stop() + messageLoop.close() + exit (0) + +# Hello World +messageLoop = asyncio.new_event_loop() +rxLoop = asyncio.ensure_future(start_rx(), loop=messageLoop) -loop = asyncio.new_event_loop() try: - loop.run_forever(start_rx()) -finally: - loop.close() + messageLoop.run_forever() +except KeyboardInterrupt: exit_handler() + pass # EOF