Skip to content

Commit 8a72f3f

Browse files
TaskManager: add stop and start functions
1 parent e24f8ef commit 8a72f3f

File tree

2 files changed

+31
-23
lines changed

2 files changed

+31
-23
lines changed

internal_filesystem/lib/mpos/main.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,9 @@ async def ota_rollback_cancel():
106106
else:
107107
mpos.TaskManager.create_task(ota_rollback_cancel()) # only gets started when mpos.TaskManager() is created
108108

109-
while True:
110-
try:
111-
mpos.TaskManager() # do this at the end because it doesn't return
112-
except KeyboardInterrupt as k:
113-
print(f"mpos.TaskManager() got KeyboardInterrupt, falling back to REPL shell...") # only works if no aiorepl is running
114-
break
115-
except Exception as e:
116-
print(f"mpos.TaskManager() got exception: {e}")
117-
print("Restarting mpos.TaskManager() after 10 seconds...")
118-
import time
119-
time.sleep(10)
109+
try:
110+
mpos.TaskManager.start() # do this at the end because it doesn't return
111+
except KeyboardInterrupt as k:
112+
print(f"mpos.TaskManager() got KeyboardInterrupt, falling back to REPL shell...") # only works if no aiorepl is running
113+
except Exception as e:
114+
print(f"mpos.TaskManager() got exception: {e}")

internal_filesystem/lib/mpos/task_manager.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,34 @@
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
89

9-
def __init__(self):
10-
print("TaskManager starting asyncio_thread")
11-
# tiny stack size of 1024 is fine for tasks that do nothing
12-
# but for real-world usage, it needs more:
13-
#_thread.stack_size(mpos.apps.good_stack_size())
14-
#_thread.start_new_thread(asyncio.run, (self._asyncio_thread(100), ))
15-
asyncio.run(self._asyncio_thread(10)) # this actually works, but it blocks the real REPL (aiorepl works, but that's limited)
16-
17-
async def _asyncio_thread(self, ms_to_sleep):
10+
@classmethod
11+
async def _asyncio_thread(cls, ms_to_sleep):
1812
print("asyncio_thread started")
19-
while True:
20-
#print("asyncio_thread tick")
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()}")
2117
await asyncio.sleep_ms(ms_to_sleep) # This delay determines how quickly new tasks can be started, so keep it below human reaction speed
22-
print("WARNING: asyncio_thread exited, this shouldn't happen because now asyncio.create_task() won't work anymore!")
18+
print("WARNING: asyncio_thread exited, now asyncio.create_task() won't work anymore")
19+
20+
@classmethod
21+
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)
24+
25+
@classmethod
26+
def stop(cls):
27+
cls.keep_running = False
28+
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
2336

2437
@classmethod
2538
def create_task(cls, coroutine):

0 commit comments

Comments
 (0)