[Date Prev][Date Next] [Thread Prev][Thread Next]
[Thread Index]
[Date Index]
[Author Index]
Re: Glib::IO watcher unbuffered?
- From: zentara <zentara1 sbcglobal net>
- To: gtk-perl-list gnome org
- Subject: Re: Glib::IO watcher unbuffered?
- Date: Fri, 19 Jan 2007 06:15:21 -0500
On Fri, 19 Jan 2007 09:24:53 +0100
"Philipp E. Letschert" <phil uni-koblenz de> wrote:
>I would like to have unbuffered output processing from an external command
>(command prints a line, sleeps, prints a line, sleeps ...):
> else {
> my $line = <$pipe>;
>I tried to have $line processed without delay (autoflush all handles
>everywhere...), but the watcher only prints bunches of lines after some longer
>delay.
>Why does the buffering happen and how to turn it off?
>
I think your problem is that <> blocks until it hits an EOF, but your
pipe never sends it. It's a common problem with running top thru
a pipe. Try using sysread:
#!/usr/bin/perl -w
use strict;
use Glib qw(TRUE FALSE);
use Gtk2 -init;
use Gtk2::Helper;
open(FH, "top -b |") or die "$!\n";
my $helper_tag = Gtk2::Helper->add_watch(fileno FH, 'in',sub{
&watch_callback('FH');
});
#standard window creation, placement, and signal connecting
my $window = Gtk2::Window->new('toplevel');
$window->signal_connect('delete_event' => sub { exit;});
$window->set_border_width(5);
$window->set_position('center_always');
my $hbox = Gtk2::HBox->new();
$hbox->set( "border_width" => 0 );
$window->add($hbox);
my $scroll = Gtk2::ScrolledWindow->new;
$scroll->set_size_request(600,600);
my $textview = Gtk2::TextView->new;
my $buffer = $textview->get_buffer;
create_tags($buffer);
$scroll->add($textview);
$hbox->pack_start($scroll,1, 1, 1 ); # expand?, fill?, padding;
$window->show_all();
#our main event-loop
Gtk2->main;
sub watch_callback {
my ($fh) = @_;
my $line;
#read 1000 caracters of the buffer
#$fh->sysread($line,1000);
sysread(FH,$line,4096);
#remove the newline
chomp $line;
if($line){
$buffer->insert_with_tags_by_name($buffer->get_end_iter, $line,'blue');
}else{
Gtk2::Helper->remove_watch($helper_tag);
}
# need the main iteration here
Gtk2->main_iteration while Gtk2->events_pending;
$textview->scroll_to_iter($buffer->get_end_iter,0,0,0,0);
#important so we can loop again
return 1;
}
#########################################################3
sub create_tags{
my $buffer = shift;
$buffer->create_tag('blue',
foreground => 'blue',
);
$buffer->create_tag('col',
foreground => 'green',
);
}
#############################################################################
__END__
zentara
--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
[Date Prev][Date Next] [Thread Prev][Thread Next]
[Thread Index]
[Date Index]
[Author Index]