[wxPython-users] Handling SIGTERM Gracefully?

Eli Golovinsky gooli at tuzig.com
Fri Feb 2 07:38:13 PST 2007


I don't know enough about the way those things work under Unix, but from =

what I recaller a signal handler behaves more or less like a short lived =

thread.

If that is the case all you need is to call wx.CallAfter and pass =

wx.GetApp().Exit as the parameter.


I've attached and example that uses a threaded timer instead of a =

signal, but it should work for a signal as well.


Hope it works.

Justin Donnelly wrote:

> I'm trying to find a way to gracefully handle SIGTERM in my wxPython
> app under Linux.  I'd like to call Close() on my main frame from
> somewhere so that its OnClose method can close a file.  Although some
> sources seem to indicate that sys.exitfunc or atexit will work when
> SIGTERM is received, those handlers don't seem to get called and I
> just see "Terminated" on the console.  I also tried setting up a
> signal handler using signal.signal.  This sort of works.  When I send
> the SIGTERM (with the kill command), nothing happens, but as soon as I
> invoke any event in my app (click on any button, for example), then my
> handler is called and the app shuts down nicely.
>
> Is there some step or method I'm missing?
>
> Thanks in advance.
>
> Justin
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: wxPython-users-unsubscribe at lists.wxwidgets.org
> For additional commands, e-mail: wxPython-users-help at lists.wxwidgets.org
>
-------------- next part --------------
import wx
import threading

class MyFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent)
        panel =3D wx.Panel(self)
        sizer =3D wx.BoxSizer(wx.VERTICAL)
        panel.SetSizer(sizer)
        label =3D wx.StaticText(panel, -1, "Hello, World!")
        sizer.Add(label, flag=3Dwx.ALIGN_CENTER|wx.ALL, border=3D20)

class MyApp(wx.App):
    def __init__(self):
        wx.App.__init__(self, redirect=3DFalse)
        self.mainframe =3D None
        =

    def OnInit(self):
        self.mainframe =3D MyFrame(None)
        self.SetTopWindow(self.mainframe)
        self.mainframe.Show()
        =

        return True

def kill_application():
    wx.CallAfter(wx.GetApp().Exit)

def main():
    app =3D MyApp()
    threading.Timer(5, kill_application).start()
    app.MainLoop()

main()


More information about the wxpython-users mailing list