Domanda

I'm making an application with socket listening with python. I have used Quickly by canonical (which use glade with gi.repository to make gui). But I can't create thread to listening function anyhow. I have tried many ways even with thread class and threading.Thread class. I'm new to Quickly and threads totally. I tried everything in internet :-) but can't find any solution. when I use thread.start() it waits till gui closes. When I use thread.run() it get out of gui and immediately runs the function which makes gui not responsive. Below is sample code I used for thread class. I can upload whole file if need since this is an open source project. Please help me.

def listen(self):
    print "working"
#listen to any incoming connections
    #self.control_sock_in.listen(1)
    i=0
    while True:
        print "it works"
    #conn,addr = self.control_sock_in.accept()
    #data = conn.recv
    #if there is any incoming connection then check for free slot and reply with that free slot
#if change of status message then update nodelist

def on_btn_create_n_clicked(self, widget):
    self.btn_quit_n.set_sensitive(True)
    self.btn_join_n.set_sensitive(False)
    self.btn_create_n.set_sensitive(False)
    subprocess.check_call(["sudo", "ifconfig", "wlan0", "192.168.0.5", "netmask", "255.255.255.0", "broadcast", "192.168.0.255"])
    self.control_sock_in = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    self.control_sock_in.bind(('192.168.0.5', 6000))
    self.status_lbl.set_text("Created the network successfully with IP of 192.168.0.5!")
    self.nodelist['192.168.0.5'] = ('6000','0')
#start thread on listen()  
    thread.start_new_thread(self.listen,(self,))
    self.btn_camera.set_sensitive(True)
    self.btn_mixer.set_sensitive(True)
    self.btn_display.set_sensitive(True)

BTW no need to provide me missing code for commented items. I can do them. What I'm stuck at is threading problem.

È stato utile?

Soluzione 2

Well I found the solution in here. link from askubuntu Have to add these two lines at beginning from gi.repository import GObject, Gtk GObject.threads_init().

Anyway huge thanks goes to Michael Hall in quickly IRC channel for pointing me the issue.

Altri suggerimenti

You can do that as easy as that

from threading import Thread
listener_thread = Thread(target=self.listen)
listener_thread.start()

When your program terminates and there are some non-daemon threads running, it will wait till they all finish.

You can flag you thread as daemon, but you need to be careful with them - see this article for more details. Basically, they might be still running and using the data that is in the consistent state because of the cleanup that is happening in other threads or already happened.

Creating a daemon thread:

from threading import Thread
listener_thread = Thread(target=self.listen)
listener_thread.daemon = True 
# or listener_thread.setDaemon(True) for old versions of python
listener_thread.start()

The best option would be to leave your listener thread non-daemon (default) and think of a way to notify a listener thread somehow when your program is about to exit. Thus your listener thread can be finished gracefully.

updated

Don't use run method of thread's object - it simply calls the function you have specified in the thread you are currently calling it from. Calling start method of Thread object fires your activity in a separate thread.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top