Gael,<div><br></div><div>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:</div>
<div><br></div><div><span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; ">Current situation<br>=============<br><br>Both matplotlib and ets have code that tries to:<br>
<br>* See what GUI toolkit is being used<br>* Get the global App object if it already exists, if not create it.<br>* See if the main loop is running, if not possibly start it.<br><br>All of this logic makes many assumptions about how IPython affects the<br>
answers to these questions.  Because IPython's GUI support has changed in significant<br>ways, current matplotlib and ets make incorrect decisions about these issues (such as trying to<br>start the event loop a second time, creating a second main App ojbect, etc.) under IPython<br>
0.11.  This leads to crashes...<br><br>Description of GUI support in 0.11<br>==========================<br><br>IPython allows GUI event loops to be run in an interactive IPython session.<br>This is done using Python's PyOS_InputHook hook which Python calls<br>
when the :func:`raw_input` function is called and is waiting for user input.<br>IPython has versions of this hook for wx, pyqt4 and pygtk.  When the inputhook<br>is called, it iterates the GUI event loop until a user starts to type<br>
again.  When the user stops typing, the event loop iterates again.  This is how tk works.<br><br>When a GUI program is used interactively within IPython, the event loop of<br>the GUI should *not* be started. This is because, the PyOS_Inputhook itself<br>
is responsible for iterating the GUI event loop.<br><br>IPython has facilities for installing the needed input hook for each GUI<br>toolkit and for creating the needed main GUI application object. Usually,<br>these main application objects should be created only once and for some<br>
GUI toolkits, special options have to be passed to the application object<br>to enable it to function properly in IPython.<br><br>What we need to decide<br>===================<br><br>We need to answer the following questions:<br>
<br>* Who is responsible for creating the main GUI application object, IPython<br> or third parties (matplotlib, enthought.traits, etc.)?<br><br>* What is the proper way for third party code to detect if a GUI application<br>
 object has already been created?  If one has been created, how should<br> the existing instance be retrieved?<br><br>* In a GUI application object has been created, how should third party code<br> detect if the GUI event loop is running. It is not sufficient to call the<br>
 relevant function methods in the GUI toolkits (like ``IsMainLoopRunning``)<br> because those don't know if the GUI event loop is running through the<br> input hook.<br><br>* We might need a way for third party code to determine if it is running<br>
 in IPython or not.  Currently, the only way of running GUI code in IPython<br> is by using the input hook, but eventually, GUI based versions of IPython<br> will allow the GUI event loop in the more traditional manner. We will need<br>
 a way for third party code to distinguish between these two cases.</span></div><div><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="border-collapse: collapse;"><br></span></font></div>
<div><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="border-collapse: collapse;">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.</span></font></div>
<div><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="border-collapse: collapse;"><br></span></font></div><div><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="border-collapse: collapse;">Cheers,</span></font></div>
<div><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="border-collapse: collapse;"><br></span></font></div><div><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="border-collapse: collapse;">Brian</span></font></div>
<div><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="border-collapse: collapse;"><br></span></font></div><div><font class="Apple-style-span" face="arial, sans-serif"><span class="Apple-style-span" style="border-collapse: collapse;"><br>
</span></font><br><div class="gmail_quote">On Sun, Jul 25, 2010 at 11:10 AM, Gael Varoquaux <span dir="ltr"><<a href="mailto:gael.varoquaux@normalesup.org">gael.varoquaux@normalesup.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
With the 0.11 series of IPython, I no longer understand how the<br>
interaction with the GUI mainloop occurs:<br>
<br>
----------------------------------------------------------------------<br>
$ ipython -wthread<br>
<br>
In [1]: import wx<br>
<br>
In [2]: wx.App.IsMainLoopRunning()<br>
Out[2]: False<br>
----------------------------------------------------------------------<br>
<br>
----------------------------------------------------------------------<br>
$ ipython -q4thread<br>
In [1]: from PyQt4 import QtGui<br>
<br>
In [2]: type(QtGui.QApplication.instance())<br>
Out[2]: <type 'NoneType'><br>
----------------------------------------------------------------------<br>
<br>
Is there a mainloop running or not? If not, I really don't understand how<br>
I get interactivity with GUI windows and I'd love an explaination or a<br>
pointer.<br>
<br>
The problem with this behavior is that there is a lot of code that checks<br>
if a mainloop is running, and if not starts one. This code thus blocks<br>
IPython and more or less defeats the purpose of the GUI options.<br>
<br>
Cheers,<br>
<br>
Gaël<br>
_______________________________________________<br>
IPython-dev mailing list<br>
<a href="mailto:IPython-dev@scipy.org">IPython-dev@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/ipython-dev" target="_blank">http://mail.scipy.org/mailman/listinfo/ipython-dev</a><br>
</blockquote></div><br><br clear="all"><br>-- <br>Brian E. Granger, Ph.D.<br>Assistant Professor of Physics<br>Cal Poly State University, San Luis Obispo<br><a href="mailto:bgranger@calpoly.edu">bgranger@calpoly.edu</a><br>
<a href="mailto:ellisonbg@gmail.com">ellisonbg@gmail.com</a><br>
</div>