|
5 | 5 | class TaskManager: |
6 | 6 |
|
7 | 7 | 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 |
9 | 9 |
|
10 | 10 | @classmethod |
11 | 11 | async def _asyncio_thread(cls, ms_to_sleep): |
12 | 12 | 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}") |
17 | 15 | await asyncio.sleep_ms(ms_to_sleep) # This delay determines how quickly new tasks can be started, so keep it below human reaction speed |
18 | 16 | print("WARNING: asyncio_thread exited, now asyncio.create_task() won't work anymore") |
19 | 17 |
|
20 | 18 | @classmethod |
21 | 19 | 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... |
24 | 26 |
|
25 | 27 | @classmethod |
26 | 28 | def stop(cls): |
27 | 29 | cls.keep_running = False |
28 | 30 |
|
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 | | - |
37 | 31 | @classmethod |
38 | 32 | def create_task(cls, coroutine): |
39 | 33 | cls.task_list.append(asyncio.create_task(coroutine)) |
|
0 commit comments