Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Responsive GUI without threads

by dk (Chaplain)
on Oct 26, 2006 at 13:21 UTC ( [id://580756]=note: print w/replies, xml ) Need Help??


in reply to Responsive GUI without threads

But one task still seems to me like one where "threads" is indeed the answer, which is making a responsive Graphical User Interface.

Doubtful at least. Why GUI programs are different from any other programs? It is quite possible to display a progress of a long process by forking it off and reading its output, the only difference that the reading pipe should be non-blocking. I don't know what class you should use for Gtk, but the idea is roughly like this:

use IO::Handle; use Fcntl; use POSIX qw(exit); $SIG{PIPE} = 'IGNORE'; my $handle = IO::Handle-> new; $handle-> autoflush(1); my $pid = open( $handle, '-|'); if ( $pid) { # parent # make it non-blocking my $fl = fcntl( $handle, F_GETFL, 0); die "$!" unless defined $fl; fcntl( $handle, F_SETFL, $fl|O_NONBLOCK) or die "$!"; # attach to your gui system so your callbacks are # pinged whenever something appears on $handle MyFictitiousGUI::FileListener->attach( $handle, on_read => sub { message( <$handle>); }, on_close => sub { message("it's over!") } ); } else { # child $|++; while ( do_computation) { print "$percents done\n"; } POSIX::_exit(0); }
Most probably POE can do this as well, but it must know about you GUI toolkit event loop structure. If you're doing it by yourself though, you should also eventually call waitpid($pid,WNOHANG) so the child process won't become a zombie.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://580756]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-04-23 19:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found