better asyncio loops

This commit is contained in:
SpudGunMan
2024-07-30 10:59:58 -07:00
parent 7e364d67d9
commit 262915adfb
3 changed files with 45 additions and 27 deletions
+1 -1
View File
@@ -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]
+15 -6
View File
@@ -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
+29 -20
View File
@@ -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