Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
After the Tk app has launched this program it sits idle listening for new ouput and updates the GUI accordingly (To emulate this you can run the second block of code, capture.pl and push the "Listen" button). I have two issues really, the first is that using the nowait flag in File::Tail causes the CPU to be hammered, but I need the method to be non-blobking so the user can still interact with the GUI. My second issue is that after the method is called, it takes several (maybe 5) seconds for the method to start tailing the file. I hope I've simplified this enough for it to make sense so you can see my issue. Maybe using File::Tail isn't the best way to go...any help would be greatly appreiciated.
# write.pl
use strict; use warnings; use IO::Handle; open(WRITE, ">>C:\\File\\test.dat"); WRITE->autoflush(1); my $num = 1; while(1) { print "$num\n"; print WRITE "Some new text $num\n"; # using sleep to try and emulate the speed of the actual program sleep 1; $num ++; } close WRITE;
# capture
use strict; use warnings; use Tk; use File::Tail; my $mw; my $name = 'C:\File\test.dat'; my $message; init_ui(); MainLoop; sub init_ui { $mw = MainWindow->new( -title => 'Test' ); $mw->resizable( 0, 0 ); my $top = $mw->Frame( )->pack( -side => 'top', -expand => '1', -fill => 'both' ); my $button1 = $top->Button( -text => "Listen", -width => '10', -command => sub { file_listen(); } )->pack( -side => 'left'); my $button2 = $top->Button( -text => "Button2", -width => '10' )->pack( -side => 'left'); my $bottom = $mw->Frame( )->pack( -side => 'top', -expand => '1', -fill => 'both' ); my $status = $bottom->Label( -textvariable => \$message, )->pack( -side => 'left' ); } sub file_listen { $message = 'Listening ... '; $mw->update; my $file=File::Tail->new(name=>$name, nowait=>1); while (defined(my $line=$file->read)) { if ($line =~ /\d+/) { chomp($line); $message = $line; } $mw->update; } }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Tk GUI and Listen?
by zentara (Archbishop) on Oct 27, 2006 at 14:07 UTC | |
by Anonymous Monk on Oct 27, 2006 at 14:29 UTC | |
by zentara (Archbishop) on Oct 27, 2006 at 14:48 UTC | |
by Anonymous Monk on Oct 27, 2006 at 15:05 UTC | |
Re: Tk GUI and Listen?
by jdtoronto (Prior) on Oct 27, 2006 at 13:39 UTC | |
by Anonymous Monk on Oct 27, 2006 at 14:14 UTC | |
by jdtoronto (Prior) on Oct 27, 2006 at 16:22 UTC | |
by Anonymous Monk on Oct 27, 2006 at 17:12 UTC | |
Re: Tk GUI and Listen?
by BrowserUk (Patriarch) on Oct 28, 2006 at 00:41 UTC |