[Tutor] threading problem in GUI

Pierre Barbier de Reuille pierre.barbier at cirad.fr
Tue Sep 6 17:32:15 CEST 2005


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
> 

-- 
Pierre Barbier de Reuille

INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP
Botanique et Bio-informatique de l'Architecture des Plantes
TA40/PSII, Boulevard de la Lironde
34398 MONTPELLIER CEDEX 5, France

tel   : (33) 4 67 61 65 77    fax   : (33) 4 67 61 56 68


More information about the Tutor mailing list