[pygtk] exception handling

John Stowers john.stowers.lists at gmail.com
Fri Jan 30 07:03:33 WST 2009


On Thu, 2009-01-29 at 12:00 -0600, John Hunter wrote:
> If I raise an exception in a gobject timeout function, is there a way
> for me to catch it hand handle it in the main thread?  Here I
> try/except the "run" method, but the exception does not propagate.  I
> recall there are difficulties in cross thread signal and exception
> handling, and am wondering what the right approach is.   I am not
> writing a UI here, just using the gobject event loop in a cron script.
>  Here is some example code

If you just want to stop the timeout from running you can return false.

If you want the program to exit then you could pass the mainloop as an
extra arg to the ontimer function, and then stop it from there, which
would exit the program

def bar(loop):
    if random.randint(0,5) == 3:
        loop.quit()
    else:
        return True

l = gobject.MainLoop()
gobject.timeout_add_seconds(1,bar,l)
l.run()

John

> 
> 
> 
> import gobject
> 
> def ontimer(*args):
>     print 'ontimer'
>     raise RuntimeError('Ouch')
>     return True
> 
> 
> gobject.timeout_add(1000, ontimer)
> 
> loop = gobject.MainLoop()
> try: loop.run()
> except RuntimeError:
>     loop.quit()
>     print 'all done'
> 
> 
> 
> 
> Using pygtk 2.10
> Thanks,
> JDH
> _______________________________________________
> pygtk mailing list   pygtk at daa.com.au
> http://www.daa.com.au/mailman/listinfo/pygtk
> Read the PyGTK FAQ: http://faq.pygtk.org/



More information about the pygtk mailing list