in reply to Re^2: Perl/Tk App and Interprocess Communication
in thread Perl/Tk App and Interprocess Communication
Okay. The code I posted will miss intermediate values if the incoming values arrive faster than the screen update checks for them.
If your application need to reflect every change, and if it can update the screen as fast or faster than the input arrives, that would not occur.
If you cannot update the screen as fast as the input arrives, then eventually you will have to either combine multiple inputs into a single update, or discard intermediates.
Either way, to ensure than your update thread sees all the inputs, it must either run faster than the inputs arrive, or they must be queued
Here is a queued solution.
#!perl -slw use strict; use threads qw[ async ]; use Thread::Queue; ## A shared var to communicate progess between work thread and TK my $Q = new Thread::Queue; sub work{ open PROC, 'perl -le"$|=1; print and select(undef,undef,undef,0.1) for 1 .. 1 +000" |' or die $!; while( <PROC> ) { $Q->enqueue( $_ ); } close PROC; } threads->new( \&work )->detach; ## For lowest memory consumption require (not use) ## Tk::* after you've started the work thread. require Tk::ProgressBar; my $mw = MainWindow->new; my $pb = $mw->ProgressBar()->pack(); my $repeat; $repeat = $mw->repeat( 100 => sub { while( $Q->pending ) { my $progress = $Q->dequeue; return unless $progress; $repeat->cancel if $progress == 100; $pb->value( $progress ) } } ); $mw->MainLoop;
|
---|