[IPython-dev] Detecting GUI mainloop running in IPython

Brian Granger ellisonbg at gmail.com
Sun Jul 25 15:10:12 EDT 2010


Great questions.  The short answer is that the traditional methods of
discovering if the event loop is running won't work.  This issue will become
even more complicated with we implement GUI integration in the new 2 process
frontend/kernel.  We still need to decide how we are going to handle this.
 Here was the last email we sent out a long time ago that didn't really get
any response:

Current situation

Both matplotlib and ets have code that tries to:

* See what GUI toolkit is being used
* Get the global App object if it already exists, if not create it.
* See if the main loop is running, if not possibly start it.

All of this logic makes many assumptions about how IPython affects the
answers to these questions.  Because IPython's GUI support has changed in
ways, current matplotlib and ets make incorrect decisions about these issues
(such as trying to
start the event loop a second time, creating a second main App ojbect, etc.)
under IPython
0.11.  This leads to crashes...

Description of GUI support in 0.11

IPython allows GUI event loops to be run in an interactive IPython session.
This is done using Python's PyOS_InputHook hook which Python calls
when the :func:`raw_input` function is called and is waiting for user input.
IPython has versions of this hook for wx, pyqt4 and pygtk.  When the
is called, it iterates the GUI event loop until a user starts to type
again.  When the user stops typing, the event loop iterates again.  This is
how tk works.

When a GUI program is used interactively within IPython, the event loop of
the GUI should *not* be started. This is because, the PyOS_Inputhook itself
is responsible for iterating the GUI event loop.

IPython has facilities for installing the needed input hook for each GUI
toolkit and for creating the needed main GUI application object. Usually,
these main application objects should be created only once and for some
GUI toolkits, special options have to be passed to the application object
to enable it to function properly in IPython.

What we need to decide

We need to answer the following questions:

* Who is responsible for creating the main GUI application object, IPython
 or third parties (matplotlib, enthought.traits, etc.)?

* What is the proper way for third party code to detect if a GUI application
 object has already been created?  If one has been created, how should
 the existing instance be retrieved?

* In a GUI application object has been created, how should third party code
 detect if the GUI event loop is running. It is not sufficient to call the
 relevant function methods in the GUI toolkits (like ``IsMainLoopRunning``)
 because those don't know if the GUI event loop is running through the
 input hook.

* We might need a way for third party code to determine if it is running
 in IPython or not.  Currently, the only way of running GUI code in IPython
 is by using the input hook, but eventually, GUI based versions of IPython
 will allow the GUI event loop in the more traditional manner. We will need
 a way for third party code to distinguish between these two cases.

While we are focused on other things right now (the kernel/frontend) we
would love to hear your thoughts on these issues.  Implementing a solution
shouldn't be too difficult.



On Sun, Jul 25, 2010 at 11:10 AM, Gael Varoquaux <
gael.varoquaux at normalesup.org> wrote:

> With the 0.11 series of IPython, I no longer understand how the
> interaction with the GUI mainloop occurs:
> ----------------------------------------------------------------------
> $ ipython -wthread
> In [1]: import wx
> In [2]: wx.App.IsMainLoopRunning()
> Out[2]: False
> ----------------------------------------------------------------------
> ----------------------------------------------------------------------
> $ ipython -q4thread
> In [1]: from PyQt4 import QtGui
> In [2]: type(QtGui.QApplication.instance())
> Out[2]: <type 'NoneType'>
> ----------------------------------------------------------------------
> Is there a mainloop running or not? If not, I really don't understand how
> I get interactivity with GUI windows and I'd love an explaination or a
> pointer.
> The problem with this behavior is that there is a lot of code that checks
> if a mainloop is running, and if not starts one. This code thus blocks
> IPython and more or less defeats the purpose of the GUI options.
> Cheers,
> Gaël
> _______________________________________________
> IPython-dev mailing list
> IPython-dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-dev

Brian E. Granger, Ph.D.
Assistant Professor of Physics
Cal Poly State University, San Luis Obispo
bgranger at calpoly.edu
ellisonbg at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20100725/4b4758b1/attachment.html>

More information about the IPython-dev mailing list