IPython - Enhanced Interactive Python

IPython hangs with PyGTK

Reported by Michiel de Hoon on 2008-09-16
26
This bug affects 4 people
Affects Status Importance Assigned to Milestone
IPython
Fix Released
High
Ville M. Vainio
ipython (Ubuntu)
Undecided
Unassigned

Bug Description

PyGTK now contains an interactive event loop. IPython should switch off this event loop before starting its own event loop to avoid the two event loops interfering with each other. The fix is trivial: Just call gtk.set_interactive(False) after importing gtk (see the patch).

Michiel de Hoon (mjldehoon) wrote :
Ville M. Vainio (villemvainio) wrote :

Before this is committed - what does this interactive event loop mean? Could it be used to avoid launching a thread for gtk event loop (which is possible with tk and qt4)?

Michiel de Hoon (mjldehoon) wrote :

> Before this is committed - what does this interactive event loop mean?

gtk.set_interactive works essentially the same as Tkinter's interactive event loop. gtk.set_interactive(True) causes gtk's event loop to run automatically whenever Python is waiting for the user to type in the next Python command. After gtk.set_interactive(False), the *automatic* launching of the event loop no longer happens. Otherwise, it has no effect on gtk's event loops.

> Could it be used to avoid launching a thread for gtk event loop (which is possible with tk and qt4)?

You mean if gtk.set_interactive(False) could interfere with launching a thread for the gtk event loop?
No, it cannot. In brief,
>>> import gtk; gtk.set_interactive(False)
with the new PyGTK is identical to
>>> import gtk
with the old PyGTK.

I have tried this patch with ipython with the new PyGTK. With this patch,

ipython -gthread
>>> import gtk
>>> w = gtk.Window()
>>> w.show()

opens an interactive gtk window as it should. Without the patch, ipython hangs after the "import gtk".

On Tue, Sep 16, 2008 at 5:11 PM, Michiel de Hoon <email address hidden> wrote:

>> Could it be used to avoid launching a thread for gtk event loop (which
> is possible with tk and qt4)?
>
> You mean if gtk.set_interactive(False) could interfere with launching a thread for the gtk event loop?
> No, it cannot. In brief,
>>>> import gtk; gtk.set_interactive(False)
> with the new PyGTK is identical to
>>>> import gtk
> with the old PyGTK.
>
> I have tried this patch with ipython with the new PyGTK. With this
> patch,
>
> ipython -gthread
>>>> import gtk
>>>> w = gtk.Window()
>>>> w.show()
>
> opens an interactive gtk window as it should. Without the patch, ipython
> hangs after the "import gtk".

I am not questioning your patch - rather, I'm thinking that if we
leave in the interactive event loop, we can make ipython interface
with gtk in a single threaded (read: robust and simple) fashion, just
like it does with tk.

This is definitely something that should be investigated.

--
Ville M. Vainio
http://tinyurl.com/vainio

Fernando Perez (fdo.perez) wrote :

On Tue, Sep 16, 2008 at 7:56 AM, Ville M. Vainio <email address hidden> wrote:

> I am not questioning your patch - rather, I'm thinking that if we
> leave in the interactive event loop, we can make ipython interface
> with gtk in a single threaded (read: robust and simple) fashion, just
> like it does with tk.
>
> This is definitely something that should be investigated.

+1: it would be a fantastic 0.10 material if we could figure out more
robust implementations of this stuff. You (Ville) also mentioned a
while ago the possibility of having tk-like versions of all the gui
support via pyoshook, perhaps accessed via ctypes. If you can have a
stab at prototyping this out, it would be great for 0.10.

Cheers,

f

Changed in ipython:
assignee: nobody → villemvainio
importance: Undecided → High
Ville M. Vainio (villemvainio) wrote :

Please run the attached file (pylabgtk.py). My gtk ( 2.12.1-0ubuntu1 ) does not have set_interactive, so you are probably using a newer version. However, if your pygtk works the way I expect it to work, you can enter plot() and continue running ipython normally...

Michiel de Hoon (mjldehoon) wrote :

> I am not questioning your patch - rather, I'm thinking that if we
> leave in the interactive event loop, we can make ipython interface
> with gtk in a single threaded (read: robust and simple) fashion, just
> like it does with tk.

Yes, that should be possible, as in principle GTK's interactive event loop works the same as Tkinter's event loop.

> Please run the attached file (pylabgtk.py). My gtk ( 2.12.1-0ubuntu1 )
> does not have set_interactive, so you are > probably using a newer
> version. However, if your pygtk works the way I expect it to work, you
> can enter plot() and continue running ipython normally...

pylabgtk.py works for me. I just needed to add gtk.main_quit() and sys.exit() inside the run_ipython function after the shell.mainloop() to make sure that ipython exits cleanly. Otherwise, it seems to work fine. The plot function brings up the matplotlib window, which is responsive, and I get the ipython prompt back without having to close the window.

Note that the set_interactive stuff is now in the bleeding-edge developer version of pygtk and not yet in an official release, and users may not yet be able to use this stuff until the next release of pygtk.

Ville M. Vainio (villemvainio) wrote :

On Wed, Sep 17, 2008 at 1:46 AM, Michiel de Hoon <email address hidden> wrote:

>> I am not questioning your patch - rather, I'm thinking that if we
>> leave in the interactive event loop, we can make ipython interface
>> with gtk in a single threaded (read: robust and simple) fashion, just
>> like it does with tk.
>
> Yes, that should be possible, as in principle GTK's interactive event
> loop works the same as Tkinter's event loop.
>
>> Please run the attached file (pylabgtk.py). My gtk ( 2.12.1-0ubuntu1 )
>> does not have set_interactive, so you are > probably using a newer
>> version. However, if your pygtk works the way I expect it to work, you
>> can enter plot() and continue running ipython normally...
>
> pylabgtk.py works for me. I just needed to add gtk.main_quit() and
> sys.exit() inside the run_ipython function after the shell.mainloop() to
> make sure that ipython exits cleanly. Otherwise, it seems to work fine.
> The plot function brings up the matplotlib window, which is responsive,
> and I get the ipython prompt back without having to close the window.
>
> Note that the set_interactive stuff is now in the bleeding-edge
> developer version of pygtk and not yet in an official release, and users
> may not yet be able to use this stuff until the next release of pygtk.

That's awesome neews.

We can finally get rid of the fragile threading stuff, once the
bleeding edge is released :-)

--
Ville M. Vainio
http://tinyurl.com/vainio

Michiel de Hoon (mjldehoon) wrote :

For the time being, can my original patch, or your code in pylabgtk.py surrounded by a try:except:, be included in the next release of ipython? Currently we are in the situation that if a new pygtk release becomes available, users may find that ipython hangs.

Ville M. Vainio (villemvainio) wrote :

On Wed, Sep 17, 2008 at 3:13 PM, Michiel de Hoon <email address hidden> wrote:
> For the time being, can my original patch, or your code in pylabgtk.py
> surrounded by a try:except:, be included in the next release of ipython?
> Currently we are in the situation that if a new pygtk release becomes
> available, users may find that ipython hangs.

Yes, I will do this. By the time new pygtk comes available we may also
add a pointer to the better, single threaded way to accomplish the
same thing.

--
Ville M. Vainio
http://tinyurl.com/vainio

Changed in ipython:
status: New → Fix Committed
Changed in ipython:
status: Fix Committed → Fix Released
dieterv (dieterv77) wrote :

Two questions:
- When will the karmic ipython package be updated?
- I didn't really understand the previous posts, but when i use the 0.9.1 ipython package in karmic, and
  start ipython with the -pylab flag, the shell starts but doesn't respond to anything inputs (see the following bug: https://bugs.launchpad.net/ubuntu/+source/ipython/+bug/408182) Is this the same issue as is being discussed here?

thanks very much

Fernando Perez (fdo.perez) wrote :

Hi,

On Wed, Sep 16, 2009 at 5:40 PM, dieterv <email address hidden> wrote:
> Two questions:
> - When will the karmic ipython package be updated?
> - I didn't really understand the previous posts, but when i use the 0.9.1 ipython package in karmic, and
>  start ipython with the -pylab flag, the shell starts but doesn't respond to anything inputs (see the following bug: https://bugs.launchpad.net/ubuntu/+source/ipython/+bug/408182)  Is this the same issue as is being discussed here?
>

Yes, indeed it looks to be the same problem. I have no idea what they
could be doing in Karmic, it sounds like a gtk/wx problem to me. I've
never seen that problem myself, but unfortunately I haven't had the
chance to test Karmic yet, I'm still on 9.04 and I don't have the time
to set up a test system right now...

We haven't heard anything from the ubuntu ipython maintainer on the
list though, it would be great if that person were to come over to
ipython-dev and give us some more details on the problem.

Sorry for not having a more useful answer right now for you.

Cheers,

f

Fernando Perez (fdo.perez) wrote :

On Sun, Sep 20, 2009 at 8:11 AM, Fernando Perez <email address hidden> wrote:

> Yes, indeed it looks to be the same problem.  I have no idea what they
> could be doing in Karmic, it sounds like a gtk/wx problem to me.  I've
> never seen that problem myself, but unfortunately I haven't had the
> chance to test Karmic yet, I'm still on 9.04 and I don't have the time
> to set up a test system right now...
>
> We haven't heard anything from the ubuntu ipython maintainer on the
> list though, it would be great if that person were to come over to
> ipython-dev and give us some more details on the problem.
>
> Sorry for not having a more useful answer right now for you.

I should add though that, as mentioned above by Ville and Brian, this
has been fixed in ipython to the best of our knowledge in 0.10. So
the only issue is one with Karmic providing an update, which we
(ipython) don't control

But do let us know if you see this problem again with an official
0.10, and we'll reopen the bug.

Cheers,

f

Michele Mattioni (mattions) wrote :

    mattions@triton:~$ ipython -pylab
    Your PyGtk has set_interactive(), so you can use the
    more stable single-threaded Gtk mode.
    See https://bugs.launchpad.net/ipython/+bug/270856
    Python 2.6.4 (r264:75706, Nov 2 2009, 14:38:03)
    Type "copyright", "credits" or "license" for more information.

    IPython 0.10 -- An enhanced Interactive Python.
    ? -> Introduction and overview of IPython's features.
    %quickref -> Quick reference.
    help -> Python's own help system.
    object? -> Details about 'object'. ?object also works, ?? prints more.

      Welcome to pylab, a matplotlib-based Python environment.
      For more information, type 'help(pylab)'.

Is that correct?
how can I make use of the interactive loop with PyGTK?

Michiel de Hoon (mjldehoon) wrote :

Leave out the -pylab when starting ipython, and then do "from pylab import *".

Best,

--Michiel

--- On Mon, 12/7/09, Michele Mattioni <email address hidden> wrote:

> From: Michele Mattioni <email address hidden>
> Subject: [Bug 270856] Re: IPython hangs with PyGTK
> To: <email address hidden>
> Date: Monday, December 7, 2009, 8:56 AM
>     mattions@triton:~$
> ipython -pylab
>     Your PyGtk has set_interactive(), so you can
> use the
>     more stable single-threaded Gtk mode.
>     See https://bugs.launchpad.net/ipython/+bug/270856
>     Python 2.6.4 (r264:75706, Nov  2 2009,
> 14:38:03)
>     Type "copyright", "credits" or "license" for
> more information.
>
>     IPython 0.10 -- An enhanced Interactive
> Python.
>     ?         ->
> Introduction and overview of IPython's features.
>     %quickref -> Quick reference.
>     help      -> Python's own
> help system.
>     object?   -> Details about
> 'object'. ?object also works, ?? prints more.
>
>       Welcome to pylab, a matplotlib-based
> Python environment.
>       For more information, type
> 'help(pylab)'.
>
> Is that correct?
> how can I make use of the interactive loop with PyGTK?
>
> --
> IPython hangs with PyGTK
> https://bugs.launchpad.net/bugs/270856
> You received this bug notification because you are a direct
> subscriber
> of the bug.
>
> Status in IPython - Enhanced Interactive Python: Fix
> Released
>
> Bug description:
> PyGTK now contains an interactive event loop. IPython
> should switch off this event loop before starting its own
> event loop to avoid the two event loops interfering with
> each other. The fix is trivial: Just call
> gtk.set_interactive(False) after importing gtk (see the
> patch).
>
> To unsubscribe from this bug, go to:
> https://bugs.launchpad.net/ipython/+bug/270856/+subscribe
>

Fernando Perez (fdo.perez) wrote :

Michiel, the problem is that in this case (I just tried) the shell blocks while plot windows are open...

For 0.10.1, it would be nice to either have a solid solution here or to remove that warning, because it's not very informative to the users. I tried changing the code to have a single-threaded shell simply calling set_interactive(True/False), but neither worked: the shell just blocks on input.

Ultimately in 0.11 we have a much more robust solution to this using PyOS_InputHook, and so far it's looking very solid. So if someone proposes for 0.10.1 a patch that takes proper advantage of the set_interactive call, I'll be happy to apply it, otherwise there's no point in giving that warning (since using the non-threaded shell and gtk just blocks, as best I can see in testing here on Ubuntu Karmic with a current pygtk that has set_interactive).

0.10 entered Ubuntu quite awhile ago. Closing the Ubuntu task.

Changed in ipython (Ubuntu):
status: New → Fix Released
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Duplicates of this bug

Other bug subscribers

Bug attachments