mirror of https://git.sr.ht/~michalr/menu
Have one worker thread instead of creating threads on demand
This commit is contained in:
parent
8fafd58b15
commit
9913ceb564
15
menu.py
15
menu.py
|
@ -214,10 +214,17 @@ class App:
|
||||||
actions))
|
actions))
|
||||||
self.board: IBoard = MenuBoard(self.get_screen_rect(), buttons, theme)
|
self.board: IBoard = MenuBoard(self.get_screen_rect(), buttons, theme)
|
||||||
self.task_q = Queue()
|
self.task_q = Queue()
|
||||||
|
self.worker_q = Queue()
|
||||||
self.screensaver = ClockScreensaver()
|
self.screensaver = ClockScreensaver()
|
||||||
self.TICKS_UNTIL_SCREENSAVER = screensaver_delay * self.FPS
|
self.TICKS_UNTIL_SCREENSAVER = screensaver_delay * self.FPS
|
||||||
if hide_cursor:
|
if hide_cursor:
|
||||||
pygame.mouse.set_visible(False)
|
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:
|
def get_screen_rect(self) -> pygame.Rect:
|
||||||
return pygame.Rect(0, 0, self.screen.get_width(), self.screen.get_height())
|
return pygame.Rect(0, 0, self.screen.get_width(), self.screen.get_height())
|
||||||
|
@ -244,9 +251,8 @@ class App:
|
||||||
finally:
|
finally:
|
||||||
self.task_q.put(end_thr)
|
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.board = MessageBoard(self.get_screen_rect(), action['action_message'], theme=self.theme)
|
||||||
|
self.worker_q.put(thr_fun)
|
||||||
return impl
|
return impl
|
||||||
|
|
||||||
def get_handler(self, action: Action) -> Callable[[], None]:
|
def get_handler(self, action: Action) -> Callable[[], None]:
|
||||||
|
@ -264,8 +270,7 @@ class App:
|
||||||
self.task_q.put(end_thr)
|
self.task_q.put(end_thr)
|
||||||
|
|
||||||
self.board = MessageBoard(self.get_screen_rect(), action['action_message'], theme=self.theme)
|
self.board = MessageBoard(self.get_screen_rect(), action['action_message'], theme=self.theme)
|
||||||
process_thr = Thread(target=thr_fun)
|
self.worker_q.put(thr_fun)
|
||||||
process_thr.start()
|
|
||||||
return impl
|
return impl
|
||||||
|
|
||||||
def button_press_handler(self, action: Action) -> Callable[[], None]:
|
def button_press_handler(self, action: Action) -> Callable[[], None]:
|
||||||
|
@ -313,11 +318,13 @@ class App:
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|
||||||
self.clock.tick(self.FPS)
|
self.clock.tick(self.FPS)
|
||||||
|
self.worker_thr.join()
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
|
|
||||||
def quit(self):
|
def quit(self):
|
||||||
self.board = MessageBoard(self.get_screen_rect(), "Exiting...", theme=self.theme)
|
self.board = MessageBoard(self.get_screen_rect(), "Exiting...", theme=self.theme)
|
||||||
self.running = False
|
self.running = False
|
||||||
|
self.worker_q.put(None)
|
||||||
|
|
||||||
|
|
||||||
def get_url_defs(config_data: dict) -> List[Action]:
|
def get_url_defs(config_data: dict) -> List[Action]:
|
||||||
|
|
Loading…
Reference in New Issue