Merge pull request #63 from SemjonKerner/call_in_thread_fifo

Make call_in_thread fifo
This commit is contained in:
Adam Saudagar 2021-04-17 13:21:17 +05:30 committed by GitHub
commit db70ae1889
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,6 +2,7 @@ import logging
import uuid import uuid
from tkinter import OptionMenu, Button, IntVar from tkinter import OptionMenu, Button, IntVar
from typing import List, Callable, Optional, Dict, Any from typing import List, Callable, Optional, Dict, Any
import queue
import threading import threading
from fishy.web import web from fishy.web import web
@ -25,8 +26,8 @@ class GUI:
self._start_restart = False self._start_restart = False
self._destroyed = True self._destroyed = True
self._log_strings = [] self._log_strings = []
self._function_queue: Dict[str, Callable] = {} self._function_queue = queue.Queue()
self._result_queue: Dict[str, Any] = {} self._result_dict: Dict[str, Any] = {}
self._bot_running = False self._bot_running = False
# UI items # UI items
@ -72,21 +73,21 @@ class GUI:
self._thread.start() self._thread.start()
def _clear_function_queue(self): def _clear_function_queue(self):
while len(self._function_queue) > 0: while not self._function_queue.empty():
_id, func = self._function_queue.popitem() _id, func = self._function_queue.get()
result = func() result = func()
self._result_queue[_id] = result self._result_dict[_id] = result
def call_in_thread(self, func: Callable, block=False): def call_in_thread(self, func: Callable, block=False):
_id = str(uuid.uuid4()) _id = str(uuid.uuid4())
self._function_queue[_id] = func self._function_queue.put((_id, func))
if not block: if not block:
return None return None
wait_until(lambda: _id in self._result_queue) wait_until(lambda: _id in self._result_dict)
return self._result_queue.pop(_id) return self._result_dict.pop(_id)
def _get_start_stop_text(self): def _get_start_stop_text(self):
return "STOP (F9)" if self._bot_running else "START (F9)" return "STOP (F9)" if self._bot_running else "START (F9)"