I'm having a performance issue updating my Tk app. I've simplified the code examples for my scenario, but basically I have a windows Tk app the launches an external program and this program continually writes all of it's output to a text file until it completes. (To emulate this you can run the first block of code, write.pl).
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;
}
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.