简体   繁体   中英

Use Thread in Pygtk

I'm in a project to develop a chat application like netmeeting. I want to separate thread one is for GUI (gtk.main) another is simultaneously accepting client chat request (socket.accept) . But there is a problem..

here is my simple line of code which is 2nd thread for accepting client request:

while True:
    self.new_sock,self.client_addr = self.sock.accept()
    #CloseDialog is a messege box
    respons=self.CloseDialog.run()
    if respons==gtk.RESPONSE_YES:
        #Call a Chat Window         
    elif respons==gtk.RESPONSE_NO:
        #Close the requested socket

when i run the application it switch to the gtk.main loop and my 2nd thread is unable to run. is there any process to run those thread parallel, But I'm very confuse to implement this. if you need any further information I will give it to you. Please help me.. thanks in advance

Have a look at PyGTK FAQ . In particular, I suggest the gobject.idle_add() approach. When your second thread needs any GUI interaction, schedule some code running in the main thread with `gobject.idle_add(), sort of like this:

def ask_close ():
    self.CloseDialog.run ()
gobject.idle_add (ask_close)

Then you'd need to wait for some mutex X for when answer (in the main thread) arrives. I also recommend not using modal dialogs ( run() ), because that largely defeats the purpose of having multiple threads. Instead, use present() and connect a callback to response signal. This callback would release mutex X so that the second thread can stop waiting and process the answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM