[Tutor] [tutor] threading problem in GUI

nephish nephish at xit.net
Wed Sep 7 16:52:34 CEST 2005



	Serial1() just call the function ... it will be evaluated and then the
	result will be sent to Thread.start ...

	So try:

	Thread.start(Serial)


ok,
so far the thread runs fine in the background, but just does not send the output to the 
textbuffer, and hangs up the rest of the show.

i think i am off in how i have the gtk.main() set up
and how the main class is set up

class SimpleGladeApp:
    def __init__(self, path, root=None, domain=None, **kwargs):        
        if os.path.isfile(path):
            self.glade_path = path
        else:
            glade_dir = os.path.dirname( sys.argv[0] )
            self.glade_path = os.path.join(glade_dir, path)
        for key, value in kwargs.items():
            try:
                setattr(self, key, weakref.proxy(value) )
            except TypeError:
                setattr(self, key, value)
        self.glade = None
        self.install_custom_handler(self.custom_handler)
        self.glade = self.create_glade(self.glade_path, root, domain)
        if root:
            self.main_widget = self.get_widget(root)
        else:
            self.main_widget = None
        self.normalize_names()
        self.add_callbacks(self)
        self.new()
	
    def main(self):
        gtk.gdk.threads_enter()
        gtk.main()
        gtk.gdk.threads_leave() 

class Main(SimpleGladeApp):
    def __init__(self, path="pivcontrolcenter.glade",
                 root="Main",
                 domain=app_name, **kwargs):
        path = os.path.join(glade_dir, path)
        SimpleGladeApp.__init__(self, path, root, domain, **kwargs)

    def on_StartEnginesButton_clicked(self, widget, *args):
        print "on_StartEnginesButton_clicked called with self.%s" % widget.get_name()        
        Input1Iter = self.Input1Buffer.get_end_iter()
        Input2Iter = self.Input2Buffer.get_end_iter()
        Input1Data = 'Reading Serial device ttyS14 \n'
        Input2Data = 'Reading Serial device ttys15 \n'
        self.Input1Buffer.insert(Input1Iter, Input1Data)
        self.Input2Buffer.insert(Input2Iter, Input2Data)        
        time.sleep(1)
        def Serial1():            
            print 'running serial 1'            
            ser = serial.Serial('/dev/ttyS15', 2400, timeout=None)
            loopy = 1
            i = 1
            while loopy < 5:                
                for x in range(5):                    
                    i = i + 1                    
                    a = ser.read(1)#read one byte        
                    a = ord(a) # change byte to integer
                    if (a < 64 )or (a > 127):
                        break                    
                    b = ser.read(1)
                    b = ord(b)
                    if (b < 64 )or (b > 127):
                        break
                    c = ser.read(1)
                    c = ord(c)
                    if c < 92:
                        break                    
                    d = ser.read(1)
                    d = ord(d)
                    if d < 128:
                        break
                    Sensor_ID = (a & 63) + (b & 63) * 64 + (c & 1) * 4096
                    Status = (c & 62) / 2 + (d & 63) * 32
                    c = int(c)
                    d = int(d)
                    x_Now = strftime('%Y-%m-%d %H:%M:%S')                   
                    #gtk.threads_enter()                    
                    Input1Data = str(Sensor_ID)+'\t'+str(Status)+'\t--------->\t'+x_Now+'\n'
                    Input1Iter = self.Input1Buffer.get_end_iter()
                    self.Input1Buffer.insert(Input1Iter, Input1Data)
                    #gtk.threads_leave()                    
                    f = open('/home/piv/PivData/tmp/Serial/'+str(i), 'w')
                    f.write(str(Sensor_ID)+'\n'+str(c)+'\n'+str(d)+'\n'+str(Status)+'\n'+x_Now)       
                    f.close()        
        Thread.start(Serial1())

def main():
    main = Main()
    #gtk.threads_enter()
    main.run()
    #gtk.threads_leave()

if __name__ == "__main__": 
    #gtk.threads_enter()   
    main()
    #gtk.threads_leave() 

lots of comments, been trying this several ways.

cheers
shawn 







More information about the Tutor mailing list