Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Tk GUI and Listen?

by BrowserUk (Patriarch)
on Oct 28, 2006 at 00:41 UTC ( [id://581046]=note: print w/replies, xml ) Need Help??


in reply to Tk GUI and Listen?

Here's a simple threaded tail and a simple Tk app that uses it.

The threaded tail doesn't chase inodes or anything fancy like that. It only displays new lines not a few existing ones (though that could be easily changed).

It uses negligable cpu with the default setting of 100 milliseconds sleep after a failed attempt to read a new line.

#!perl -slw use strict; package threads::Tail; use threads; use threads::shared; use Thread::Queue; our @ISA = 'Thread::Queue'; my $die:shared = 0; sub tail{ my( $Q, $filename, $delay ) = @_; open my $fh, '<', $filename or die "$filename: $!"; seek $fh, 0, 2; until( $die ) { my $line = <$fh>; Win32::Sleep( $delay ), next unless defined $line; $Q->enqueue( $line ); } } sub new { my( $class, $filename, %args ) = @_; my $Q = new Thread::Queue; threads->new( \&tail, $Q, $filename, $args{ delay } || 100 )->deta +ch; return bless $Q, $class; } sub DESTROY { $die = 1; sleep 1; return; } 1; package main; #use threads::Tail; my $Q = threads::Tail->new( $ARGV[ 0 ], delay => 100 ); require Tk::Text; my $mw = MainWindow->new( -width => 400, -height => 400 ); my $text = $mw->Text( -wrap => 'none', -height => 20, )->pack( -expand => 1 ); my $repeat; $repeat = $mw->repeat( 100 => sub { while( $Q->pending ) { my $line = $Q->dequeue; return unless $line; $text->insert( 'end', $line ); $text->yview( 'end' ); } } ); $mw->MainLoop;

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-04-16 20:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found