0

In Python I have my main program which is basically a console 'gui' class. Its main method is a running thread that keeps the mains screen with options printed, and keeps waitin for user to input a choice.

The gui class has another object initialized that has its own running Threads. One of the Threads in this other object basically requires the main GUI thread to switch to a different mode / stop, but because it always 'pauses' on any input(), it will never switch.

So is there way, since I have access to the gui thread from the other object, to send an empty stdin to the gui thread so it gets over input()?

Example: (not actual code)

from threading import Thread

class Gui:

  def __init__(self):
    self.mainthread = Thread(target=self.console_loop, daemon=True)
    self.server = Server(self.mainthread)
    self.mainthread.start()
    self.mainthread.join()

  def console_loop(self):
    while True:
      if some_terminating_condition:
        break
      while self.server.guiloop:
        """
        .
        . various code
        .
        """
        a = input()
        if a == "Some input":
          self.server.guiloop = False
          self.server.do_stuff()
      while not self.server.guiloop:
        """
        More code
        """


class Server:

  def __init__(self, guithread):
    self.guiloop = True
    self.gui_thread = guithread

  def do_stuff(self):
    self.guiloop = False
    """
    Code to stop gui waiting for input <-- What I need
    """
    # Arbitrary code


Sorry for any mistakes, wrote it on the fly, since my code is too big to copy over

martineau
  • 119,623
  • 25
  • 170
  • 301
Jack Avante
  • 1,405
  • 1
  • 15
  • 32
  • You might find my answer to [Freezing/Hanging tkinter Gui in waiting for the thread to complete](https://stackoverflow.com/questions/53696888/freezing-hanging-tkinter-gui-in-waiting-for-the-thread-to-complete) helpful. – martineau Oct 18 '19 at 10:22
  • Thanks Martin! I've looked into it and I'll try to apply it here. My worry is that it's a simple console with text and most options print further prompts. I'm not sure how I would know to redirect the queue messages to proper variables when certain options are selected with respective prompts being on the screen before proceeding to refresh the main 'hub' with active information shown. This seems much more suited for an actual tkinter gui where there is no thread being blocked by an STDIN request, and all text can be freely written into respective places – Jack Avante Oct 18 '19 at 12:30
  • Jack: The takeaway that might apply here is using a `Queue` to communicate between threads and periodically polling it in a non-blocking way. You can put whatever you want in it including info that would allow other data in it to be redirected properly on the receiving end. – martineau Oct 18 '19 at 15:27
  • I see. I will go implement that right now and see how I can work with it, thank you – Jack Avante Oct 18 '19 at 16:26

0 Answers0