TaskManager: simplify

This commit is contained in:
Thomas Farstrike
2025-12-11 21:11:59 +01:00
parent 8a72f3f343
commit 3cd66da3c4
2 changed files with 11 additions and 17 deletions
+2 -2
View File
@@ -90,9 +90,9 @@ if auto_start_app and launcher_app.fullname != auto_start_app:
# Create limited aiorepl because it's better than nothing:
import aiorepl
async def asyncio_repl():
print("Starting very limited asyncio REPL task. Use sys.exit() to stop all asyncio tasks and go to real REPL...")
print("Starting very limited asyncio REPL task. To stop all asyncio tasks and go to real REPL, do: import mpos ; mpos.TaskManager.stop()")
await aiorepl.task()
mpos.TaskManager.create_task(asyncio_repl()) # only gets started when mpos.TaskManager() is created
mpos.TaskManager.create_task(asyncio_repl()) # only gets started when mpos.TaskManager.start() is created
async def ota_rollback_cancel():
try:
+9 -15
View File
@@ -5,35 +5,29 @@ import mpos.apps
class TaskManager:
task_list = [] # might be good to periodically remove tasks that are done, to prevent this list from growing huge
keep_running = True
keep_running = None
@classmethod
async def _asyncio_thread(cls, ms_to_sleep):
print("asyncio_thread started")
while TaskManager.should_keep_running() is True:
#while self.keep_running is True:
#print(f"asyncio_thread tick because {self.keep_running}")
print(f"asyncio_thread tick because {TaskManager.should_keep_running()}")
while cls.keep_running is True:
#print(f"asyncio_thread tick because {cls.keep_running}")
await asyncio.sleep_ms(ms_to_sleep) # This delay determines how quickly new tasks can be started, so keep it below human reaction speed
print("WARNING: asyncio_thread exited, now asyncio.create_task() won't work anymore")
@classmethod
def start(cls):
#asyncio.run_until_complete(TaskManager._asyncio_thread(100)) # this actually works, but it blocks the real REPL (aiorepl works, but that's limited)
asyncio.run(TaskManager._asyncio_thread(1000)) # this actually works, but it blocks the real REPL (aiorepl works, but that's limited)
cls.keep_running = True
# New thread works but LVGL isn't threadsafe so it's preferred to do this in the same thread:
#_thread.stack_size(mpos.apps.good_stack_size())
#_thread.start_new_thread(asyncio.run, (self._asyncio_thread(100), ))
# Same thread works, although it blocks the real REPL, but aiorepl works:
asyncio.run(TaskManager._asyncio_thread(10)) # 100ms is too high, causes lag. 10ms is fine. not sure if 1ms would be better...
@classmethod
def stop(cls):
cls.keep_running = False
@classmethod
def should_keep_running(cls):
return cls.keep_running
@classmethod
def set_keep_running(cls, value):
cls.keep_running = value
@classmethod
def create_task(cls, coroutine):
cls.task_list.append(asyncio.create_task(coroutine))