Chat box locking up with multiple clients running - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Networking (https://python-forum.io/forum-12.html) +--- Thread: Chat box locking up with multiple clients running (/thread-22731.html) |
Chat box locking up with multiple clients running - Clunk_Head - Nov-24-2019 I am working on a GUI based chat program. I am using someone else's server which has worked well for many people so I am assuming the problem is with my client's code. When I run a single instance of the client it works perfectly, but if I run two instances of the client on the same computer the listener stops responding when the 2nd client logs in. # server is from socket module # chat_box is a tkinter ListBox class listener_thread(threading.Thread): def __init__(self, server, chat_box): super(listener_thread, self).__init__() self.server = server self.chat_box = chat_box def run(self): try: update = self.server.recv(1024) msg = update.decode("utf-8") if msg != "": self.chat_box.insert(END, msg) except Exception as e: print(e)I've verified that the server is putting each client on a different port. The server is receiving the messages. When 'Michael' logs in and says 'Hi' it updates in his chat_box. Though, the clients are no longer updating their histories after 'Dave' logs in. Yet, the server continues to show that it is receiving the messages from both clients. I'm not sure on which board I should place this question; it might be networking or it might be threading. The network connection is working properly. It just locks up the list_box updating threads. No exceptions are being thrown. RE: Chat box locking up with multiple clients running - Clunk_Head - Nov-27-2019 I solved my own problem. I needed to make the chat_history_listbox as a ListBox initially, instead of None I needed to put the receive code into a function, with a loop and an exit condition def receive_func(): global server, chat_history_listbox while True: try: update = server.recv(1024) except OSError as e: update = None break connect() msg = update.decode("utf-8") if msg != "": chat_history_listbox.insert(END, msg)I needed to make the thread call a function and make it a daemon listener = Thread(target=receive_func, daemon=True) listener.start()This got it working with multiple clients |