Search code examples
socketspython-2.7python-multithreadingcanonical-quickly

Do threading (socket listening) in python with Quickly


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.


Solution

  • 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.