Domanda

I'm just experimenting with this. Whenever a user opens the program he should get 'online' and listen for connections.
Here the GUI gets loaded.

class AppUI(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent
        self.initUi()

    def initUi(self):
        self.parent.title("Redux")
        self.pack(fill=BOTH, expand=1)
        self.initMenu()
        self.initAudio()
        self.initMidi()
        self.initBroadcast()
        self.initFriendList()

But whenever I paste the code of my thread under initUi, it get's stuck on loading and my GUI doesn't show up. (keeps loading, because the thread is listening to connections)

thread2 = threading.Thread(target=Connection().getOnline("", 50007))
thread2.start()

Class Connection():
    def getOnline(self, host, port):
        self.s.bind((host, port))
        self.s.listen(2)
        print('Now online - listening to connections')
        conn, addr = self.s.accept()
        print("Connected to:", addr)

Why is my thread not working?

È stato utile?

Soluzione

Your trouble is in this line:

thread2 = threading.Thread(target=Connection().getOnline("", 50007))

Here, you're actually calling Connection().getOnline("", 50007), which blocks. You haven't done this in the background, you've done it before your thread is started. You need to adjust your call to look like this:

thread2 = threading.Thread(target=Connection().getOnline, args = ("", 50007))
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top