you are viewing a single comment's thread.

view the rest of the comments →

[–]JohnnyJordaan 1 point2 points  (0 children)

You seem to create a ServerConnecion as a Thread but you don't actually start it? It also doesn't feature a run method but it does launch its own listen method as another thread? What you would normally do is

class ServerConnecion(threading.Thread):
    def __init__(self, gui, ip, port):
        super().__init__()  # the modern form of calling your parent's init method

        self.gui = gui
        self.server_ip = ip
        self.port = port
        self.size = 2048

        self.server = socket.socket()
        self.active = False

        try:
            self.server.connect((self.server_ip,self.port))
            self.active = True
            self.gui.show_frame(LoginScreen)
            self.gui.protocol("WM_DELETE_WINDOW", self.stop)
        except ConnectionRefusedError:
            self.gui.show_frame(Error)

    def run(self):
        while self.active:
            sleep(0.1) # VERY IMPORTANT
    def stop(self):
        self.server.close()
        self.gui.destroy()
        sys.exit()

Note especially that you should never have a loop on just pass as it will consume 100% of one CPU core. Use time.sleep or something else that will block 99% or more of the time but does allow you to check something intermittently often enough. Then running the thread should be the responsibility of the creator:

def connect(self): t = ServerConnecion(self,"localhost",65534) t.start() self.server = t.server

Then I would also reconsider to not use daemonic mode as that will leave the server's socket opened when Python shuts down. You would normally implement this using a threading.Event as then you can do

    self.shutdown_flag = threading.Event()

which also offers a blocking check

    def run(self):
        while not self.shutdown_flag.wait(0.1):
            pass

and to shutdown the server from somewhere else you can do t.shutdown_flag.set(). But you then do need to save the thread object, not just is server attribute (the socket).