[pygtk] Conflict: raw_input(), SIGALRM and pygtk
Ilya Murav'jov
muravev at yandex.ru
Wed Jun 30 23:14:58 WST 2010
Michiel de Hoon пишет:
> The problem is related to how the event loop works. Python (unlike for example Tcl/Tk) does not provide event loop support. Each GUI library (such as PyGTK) therefore implements its own event loop (and is unaware of other event loops). The PyGTK event loop is started automatically when you import gtk. The PyGTK event loop is not aware that you are using the signal module in Python, so it will not call catcher.
>
> You can switch off the PyGTK event loop by calling gtk.set_interactive(False) just after importing gtk. If you do so, you will see that catcher is called again.
>
> However, if you plan to build a PyGTK GUI around your application, then you will have to call gtk.mainloop() at the end of your script, and you will run into the same problem. The solution is then to either convince the Python developers to add event loop support to Python, or to use gtk's machinery to set up alarm signals instead of using Python's signal module:
>
> import gobject
>
> def catcher():
> print "beat!"
> return True
>
> if __name__ == "__main__":
> import gtk
> source = gobject.timeout_add(3000, catcher)
> raw_input() # to expect "beat!" printing until pressing Enter
> gobject.source_remove(source)
>
>
>
Thank you, you save my day ... no, my week of gtk coding. :)
I know about event loops but I couldn't expect that gtk_main() begins to
work behind the raw_input() (by setting PyOS_InputHook which call I
missed in readline_until_enter_or_signal())! Seriously, it is surprising
and I certainly throw away those unix signals for well-known Gtk timeout
signals.
By the way, I don't see a reason for adding event loop support to
Python,- which one (gtk, qt, GetMessage, ...)? They are very different
and ... the strongest will win :) ; here is the topic about it,
http://mail.python.org/pipermail/python-dev/2007-February/071100.html
As for raw_input(), I would like it to be more flexible than now,- not
just one call with its own event loop (which can be overrided by
PyOS_InputHook = hack, one can say) but some nice API so everyone may
realize event loop its own way (= flexible wrapper around GNU readline).
Just thoughts ...
Regards,
Ilya Murav'jov
More information about the pygtk
mailing list