[tpm] buffering problem on input from shell command

John Macdonald john at perlwolf.com
Sun Mar 9 19:06:37 PDT 2008


On Fri, Mar 07, 2008 at 09:26:39PM -0500, Madison Kelly wrote:
> Hi all,
> 
>    Another question, if I may. :)
> 
>    I've been struggling with a buffering problem... I listen to the 
> output from a shell command ('dbus-monitor --system --profile', 
> specifically). When certain strings come up, I need to perform some 
> tasks. The problem is, the data coming in is cached.
> 
>    I figured it was the usual "suffering from buffering" issue, and 
> tried various things I could to make the filehandle hot. Finally in 
> reading some of the docs I realized that making a filehandle hot only 
> works on output, not input. The only option I saw there was 'getc()', 
> but if I understand that right, it will flush after every byte, where I 
> want to flush on newline '\n'... Is this possible? Am I approaching this 
> the wrong way entirely?

The problem will be that the output is being buffered by the program 
that is writing the data you are trying to read.  That program collects
a full buffer of data before it writes it out into the pipeline.  No
matter how fast you read the other end of the pipe, you can't read the
parts that are still in the other programs buffer and not yet written
- unless you have access to the code of that program to set its output
buffering to unbuffered.

>    Here is the exact code... (cleaned up for the post)
> 
> my $shell_call="dbus-monitor --system --profile 2>&1 |";
> my $dm=IO::Handle->new();
> my $child_pid=getc ($dm, $shell_call) or die "Error: $!\n";
> $dm->autoflush(1);
> push (@::pids, $child_pid);
> while (<$dm>)
> {
> 	chomp;
> 	my $line=$_;
> 	$line=~s/\s+/ /g;
> 	my ($type, $unix_time, $serial, $unknown1, $udi, $interface, 
> $action)=split/ /, $line, 7;
> 	next if ( $type ne "sig" );
> 	next if ( $udi !~ /\/org\/freedesktop\/Hal/ );
> 	next if ( $udi =~ /acpi_BAT(\d+)$/ );
> 	
> 	# Go ahead and scan.
> 	&scan_devices($conf, $dbh);
> }
> $dm->close();
> 
>    Thanks!
> 
> Madi
> _______________________________________________
> toronto-pm mailing list
> toronto-pm at pm.org
> http://mail.pm.org/mailman/listinfo/toronto-pm


More information about the toronto-pm mailing list