1
0
Fork 0

Have one worker thread instead of creating threads on demand

This commit is contained in:
Michał Rudowicz 2024-09-13 21:46:03 +02:00
parent 8fafd58b15
commit 9913ceb564
1 changed files with 11 additions and 4 deletions

15
menu.py
View File

@ -214,10 +214,17 @@ class App:
actions))
self.board: IBoard = MenuBoard(self.get_screen_rect(), buttons, theme)
self.task_q = Queue()
self.worker_q = Queue()
self.screensaver = ClockScreensaver()
self.TICKS_UNTIL_SCREENSAVER = screensaver_delay * self.FPS
if hide_cursor:
pygame.mouse.set_visible(False)
self.worker_thr = Thread(target=self.worker)
self.worker_thr.start()
def worker(self):
for job in iter(self.worker_q.get, None):
job()
def get_screen_rect(self) -> pygame.Rect:
return pygame.Rect(0, 0, self.screen.get_width(), self.screen.get_height())
@ -244,9 +251,8 @@ class App:
finally:
self.task_q.put(end_thr)
process_thr = Thread(target=thr_fun)
process_thr.start()
self.board = MessageBoard(self.get_screen_rect(), action['action_message'], theme=self.theme)
self.worker_q.put(thr_fun)
return impl
def get_handler(self, action: Action) -> Callable[[], None]:
@ -264,8 +270,7 @@ class App:
self.task_q.put(end_thr)
self.board = MessageBoard(self.get_screen_rect(), action['action_message'], theme=self.theme)
process_thr = Thread(target=thr_fun)
process_thr.start()
self.worker_q.put(thr_fun)
return impl
def button_press_handler(self, action: Action) -> Callable[[], None]:
@ -313,11 +318,13 @@ class App:
pygame.display.flip()
self.clock.tick(self.FPS)
self.worker_thr.join()
pygame.quit()
def quit(self):
self.board = MessageBoard(self.get_screen_rect(), "Exiting...", theme=self.theme)
self.running = False
self.worker_q.put(None)
def get_url_defs(config_data: dict) -> List[Action]: