Skip to content

Commit 3cd66da

Browse files
TaskManager: simplify
1 parent 8a72f3f commit 3cd66da

File tree

2 files changed

+11
-17
lines changed

2 files changed

+11
-17
lines changed

internal_filesystem/lib/mpos/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ def custom_exception_handler(e):
9090
# Create limited aiorepl because it's better than nothing:
9191
import aiorepl
9292
async def asyncio_repl():
93-
print("Starting very limited asyncio REPL task. Use sys.exit() to stop all asyncio tasks and go to real REPL...")
93+
print("Starting very limited asyncio REPL task. To stop all asyncio tasks and go to real REPL, do: import mpos ; mpos.TaskManager.stop()")
9494
await aiorepl.task()
95-
mpos.TaskManager.create_task(asyncio_repl()) # only gets started when mpos.TaskManager() is created
95+
mpos.TaskManager.create_task(asyncio_repl()) # only gets started when mpos.TaskManager.start() is created
9696

9797
async def ota_rollback_cancel():
9898
try:

internal_filesystem/lib/mpos/task_manager.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,29 @@
55
class TaskManager:
66

77
task_list = [] # might be good to periodically remove tasks that are done, to prevent this list from growing huge
8-
keep_running = True
8+
keep_running = None
99

1010
@classmethod
1111
async def _asyncio_thread(cls, ms_to_sleep):
1212
print("asyncio_thread started")
13-
while TaskManager.should_keep_running() is True:
14-
#while self.keep_running is True:
15-
#print(f"asyncio_thread tick because {self.keep_running}")
16-
print(f"asyncio_thread tick because {TaskManager.should_keep_running()}")
13+
while cls.keep_running is True:
14+
#print(f"asyncio_thread tick because {cls.keep_running}")
1715
await asyncio.sleep_ms(ms_to_sleep) # This delay determines how quickly new tasks can be started, so keep it below human reaction speed
1816
print("WARNING: asyncio_thread exited, now asyncio.create_task() won't work anymore")
1917

2018
@classmethod
2119
def start(cls):
22-
#asyncio.run_until_complete(TaskManager._asyncio_thread(100)) # this actually works, but it blocks the real REPL (aiorepl works, but that's limited)
23-
asyncio.run(TaskManager._asyncio_thread(1000)) # this actually works, but it blocks the real REPL (aiorepl works, but that's limited)
20+
cls.keep_running = True
21+
# New thread works but LVGL isn't threadsafe so it's preferred to do this in the same thread:
22+
#_thread.stack_size(mpos.apps.good_stack_size())
23+
#_thread.start_new_thread(asyncio.run, (self._asyncio_thread(100), ))
24+
# Same thread works, although it blocks the real REPL, but aiorepl works:
25+
asyncio.run(TaskManager._asyncio_thread(10)) # 100ms is too high, causes lag. 10ms is fine. not sure if 1ms would be better...
2426

2527
@classmethod
2628
def stop(cls):
2729
cls.keep_running = False
2830

29-
@classmethod
30-
def should_keep_running(cls):
31-
return cls.keep_running
32-
33-
@classmethod
34-
def set_keep_running(cls, value):
35-
cls.keep_running = value
36-
3731
@classmethod
3832
def create_task(cls, coroutine):
3933
cls.task_list.append(asyncio.create_task(coroutine))

0 commit comments

Comments
 (0)