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; } }

In reply to Tk GUI and Listen? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.