|
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 | 9 |
|
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): |
18 | 12 | 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()}") |
21 | 17 | 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 |
23 | 36 |
|
24 | 37 | @classmethod |
25 | 38 | def create_task(cls, coroutine): |
|
0 commit comments