[Tutor] threading problem in GUI

nephish nephish at xit.net
Tue Sep 6 18:41:01 CEST 2005


Pierre Barbier de Reuille wrote:

>nephish a écrit :
>  
>
>>Hello there !
>>    
>>
>
>Hello,
>
>  
>
>>i am having a problem with threading.
>>OK, i have this GUI app that i am building with pygtk.
>>there is a process (four actually, just working on getting one right now)
>>that needs to run in the background.
>>    
>>
>
>Please, do not mix "process" and "threads" ... there very different ...
>you're talking about threads here, so you want Threads to run in the
>background ...
>
>  
>
>>there is a button that starts the background function. But, it locks up 
>>the gui. it doesn't run in the background, it locks everything up. It 
>>still runs though.
>>    
>>
>
>That's just normal ... if you read PyGtk documentation, you'll see you
>need to initialise PyGtk to handle threads :
>
>http://www.async.com.br/faq/pygtk/index.py?req=index -> 20. The GTK
>Mainloop and Threading
>
>http://www.async.com.br/faq/pygtk/index.py?req=show&file=faq20.006.htp
>-> This is exactly your problem !
>
>http://www.pygtk.org/pygtk2reference/gdk-functions.html#function-gdk--threads-init
>
>This is done like this :
>
># First call to PyGtk function ever
>gtk.gdk.threads_init()
># Here initialize what you want
>[...]
># Launch the Gtk loop
>gtk.gdk.threads_enter() # Unneeded if you don't want to call GUI
>                        # functions from other threads
>gtk.main()
>gtk.gdk.threads_leave() # Needed only with threads_enter
>
>  
>
>>one of the things this background process is to do is updata a viewable
>>area on the GUI. Now when run from a terminal, when i hit CTRL+C
>>it stops the thread, but doesnt kill the GUI, and the TextView gets 
>>updated right then with everything it should have gotten before.
>>
>>
>>def Serial1():
>>            print 'running serial 1'
>>            ser = serial.Serial('/dev/ttyS15', 2400, timeout=None)
>>            loopy = 1
>>            i = 1
>>            while loopy < 5:   
>>                x_Now = strftime('%Y-%m-%d %H:%M:%S')
>>                i = i + 1                   
>>                a = ser.read(1)#read one byte       
>>                a = ord(a) # change byte to integer
>>                if (a < 64 )or (a > 127):
>>                    continue       
>>                b = ser.read(1)
>>                b = ord(b)
>>                if (b < 64 )or (b > 127):
>>                    continue
>>                c = ser.read(1)
>>                c = ord(c)
>>                if c < 92:
>>                    continue
>>                d = ser.read(1)
>>                d = ord(d)
>>                if d < 128:
>>                    continue
>>                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')
>>                f = open('/home/piv/PivData/tmp/Serial/'+str(i), 'w')
>>                Input1Data = 
>>str(Sensor_ID)+'\t'+str(Status)+'\t--------->\t'+x_Now+'\n'
>>                Input1Iter = self.Input1Buffer.get_end_iter()
>>                self.Input1Buffer.insert(Input1Iter, 
>>Input1Data)               
>>                
>>f.write(str(Sensor_ID)+'\n'+str(c)+'\n'+str(d)+'\n'+str(Status)+'\n'+x_Now)       
>>
>>                f.close()
>>        thread.start_new(Serial1())
>>
>>the code may not be best form, i am still fairly new at this. so i am 
>>also open to
>>any constructive critisism.
>>
>>thanks
>>shawn
>>_______________________________________________
>>Tutor maillist  -  Tutor at python.org
>>http://mail.python.org/mailman/listinfo/tutor
>>
>>    
>>
>
>  
>
ok, i am still having a little problem understanding.
tried it but i don't know if i have things set in the right order.

 
gtk.gdk.threads_init()
# Here initialize what you want
[...]
# Launch the Gtk loop
gtk.gdk.threads_enter() # Unneeded if you don't want to call GUI
                        # functions from other threads
gtk.main()
gtk.gdk.threads_leave() # Needed only with threads_enter

at the part where you wrote 
# Here initialize what you want
[...]
is that where i define the function that will run the thread?

i get the part about having the enter and leave
i just seem to have a hang up (so to speak) with where the function gets defined.
do i need to build it as a class like the example in the link you sent?

thanks for your help on this




More information about the Tutor mailing list