Re: Event loop, non-blocking i/o




On Dec 11, 2004, at 1:30 PM, Martin Junius wrote:

Hi all!
[snip description of UI with update loops, nonblocking i/o, and IO::Select]
This all seems to work, but is not as fast as without the non-blocking stuff. So I'm wondering: is there a better way to do these kind of things in the world of Gtk2-Perl, maybe with some support from Glib?

GLib's main loop includes support for selecting/polling file descriptors directly, using IO watches. You add to the main context an event source which fires a callback when something happens on a file descriptor -- data arrived, writing is possible, the other end hung up, and i think one other (something to do with errors). GTK+ uses this to wait for events to show up from the X server.

gtk2-perl takes the approach that we don't bind functionality that overlaps with what perl already provides, so 99% of the GIOChannel functions are not bound; the one that we do bind is g_io_add_watch() (as Glib::IO->add_watch()), which is used to add your own file descriptor to the event loop. Because of how the underlying code works, you must pass the file descriptor, not the perl file handle (e.g., fileno(FILEHANDLE)), and use sysread() to avoid perl's IO buffering causing hard-to-diagnose problems in your handlers.

Basically, replace your usage of IO::Select and "Gtk2->main_iteration while Gtk2->events_pending" with a Glib IO watch, and write your handler as a state machine.

The FAQ has an entry about this (http://gtk2-perl.sourceforge.net/faq/#43), as do several threads in the list archives (as usual, use google rather than gnome.org's list archive search).


--
Our enemies are innovative and resourceful, and so are we. They never stop thinking about new ways to harm our country and our people, and neither do we.
  -- President George W. Bush




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]