Will python ever have signalhandlers in threads?

Fernando Perez fperez528 at yahoo.com
Fri Nov 12 19:40:20 EST 2004


Jean Brouwers wrote:

> 
> Fernando,
> 
> Just curious, does 'gtk/WX' in your message below mean wxPython?

Yes.

> If so, does this signal handling code actually work with wxPython?> 

It does, but not in a generic manner: this is code for ipython to support
matplotlib's WX backend in an interactive shell.  It allows you to type into
ipython plotting commands which cause matplotlib to open a WX plotting window,
and the interactive terminal continues to function.  You can have multiple WX
plotting windows open, and the command line keeps on chugging.

But this relies on a special collaborative hack between matplotlib and ipython.
matplotlib, in its WX and GTK backends (Tk doesn't need this) has a special
flag to indicate who is in control of the mainloop.  In standalone scripts,
everything works in the typical manner.  But if ipython comes in, it will set
this flag, telling matplotlib to keep off the mainoop.

It's pretty hackish, but it works in practice pretty well.  

here's the relevant matplotlib WX code (trimmed of docstring):

def show():

    for figwin in Gcf.get_all_fig_managers():
        figwin.frame.Show()
        figwin.canvas.realize()
        figwin.canvas.draw()

    if show._needmain and not matplotlib.is_interactive():
        wxapp.MainLoop()
        show._needmain = False
show._needmain = True

When ipython starts up, it sets show._needmain to False, so that the Mainloop()
call is never made.  You can look at the whole code from ipython if you wish,
it's in IPython/Shell.py.

best,

f




More information about the Python-list mailing list