I am spawning two threads:
a) for user interaction "GUI", pyside2 MainWindow(QtWidgets.QMainWindow) and
b) for visualisation "VIZ" , pygame
Each thread is given two queues "Qin" and "Qout" for inter-thread communication.
My goal is to trigger actions on either side upon receiving messages in the thread's "Qin" which have been sent from the other thread's "Qout".
My approach so far seems a bit "hacky" and I am seeking advice if there may be a better design pattern. I am aware that pygame itself is not well threaded optimized but I do not want to use something else.
What I do:
Within the "VIZ" main loop I read the Q and take action like this
if not queue_in.empty():
cmd, args = queue_in.get(False)
print(f"[V] {cmd=}, {args=}")
if args:
globals()[cmd](*args)
else:
globals()[cmd]()
I take a similar approach on the "GUI" side
Since - for whatever reason - globals()[cmd](*args) does not work , within __init__() I have defined
self.callables = {
"quit_menu": self.quit_menu,
"fülle_Verbrauchseinheiten": self.fülle_Verbrauchseinheiten,
}
I read the Qin via
self.timer = QTimer(self)
self.timer.timeout.connect(self.receive_state)
self.timer.start()
def receive_state(self):
if not self.queue_in.empty():
cmd, args = self.queue_in.get(False)
print(f"[G] {cmd=}, {args=}")
if args:
self.callables[cmd](*args)
else:
self.callables[cmd]()
Effectively I am calling a function/method in a thread by sending the call (with args) through the Q.
As I have pointed out: This seems hacky and maybe "overcomplicated". Before I proceed I want to make sure that I am going in the right direction.