1. Streamreader capturing UART sensor data
2. Task updating display
3. Tasks sending data over modem
If a task crashes, what would be the best way to detect this and correct the behaviour. Should a 4th task be created that monitors the first 3 tasks and restarts failed tasks? What if the 4th task crashes, how deep should one go..
Currently I create the 3 tasks, append them to a loop and await all tasks, if any task has an unhandled error I reboot the mcu. Works but is pretty crude.
Code: Select all
async def main_loop():
set_global_exception()
tasks = []
# Sensor task
m = asyncio.create_task(sensor_process())
await asyncio.sleep(0)
tasks.append(m)
# Connect modem and send MQTT
s = asyncio.create_task(gsm_run())
tasks.append(s)
# LCD update
m = asyncio.create_task(screen_update())
tasks.append(m)
await asyncio.sleep(0)
for p in tasks:
await p
try:
logger.debug('starting event loop')
asyncio.run(main_loop())
except KeyboardInterrupt:
print('Interrupted')
except Exception as e:
print("caught")
print_exception(e)
finally:
asyncio.new_event_loop()
logger.warning('ended event loop')
logger.error('WE SHOULD NEVER GET HERE')
time.sleep(5)
logger.error('REBOOTING')
machine.reset()