Re: trouble with reading from pipes




Daniel Davidson said:
This may be a problem with my understanding of perl and not with GTK,
but here goes anyway.

Actually, it's a subtle interaction of various pieces.


$tag = Gtk2::Helper->add_watch(fileno($fractread), 'in', \&setstatus);
sub setstatus{
      $progressbar->set_text(<$progread>);
      $progressbar->set_fraction(<$fractread>);

Note blocking buffered reads.  See below.

      Gtk2->main_iteration while Gtk2->events_pending;
      print "in status\n";

Watch out -- you're implicitly returning nonzero here because the print is
your block's last statement.  HOwever, the return value of the callback is
supposed to be a boolean indicating whether the callback is to stay installed.

}

And finally in the child process I print to the pipe.
      print $fractwrite $count/$totaldays;
      print $statuswrite "Populating Database $count of $totaldays
".int($count/$totaldays*100)."%";

Note lack of a newline to flush the output buffer.


Only problem is that nothing ever happens, even though I know the child
process is running.  Any ideas what I am screwing up?

Don't use buffered IO and blocking reads with IO watches.  The IO watch fires
when *real* input arrives, and using buffered IO with that messes with your
mind.  Also, the <> operator will attempt to read up to a $/, and will block
until it does.  By default, $/ is "\n", and you're not writing a "\n" to your
output stream.

So, your callback is being invoked once, and is hanging on the read, waiting
for a newline to show up.


Set $|=1 (autoflush) in the child, and use sysread() in your IO watch
callbacks.  By the way, when using sysread(), you must be prepared to handle
partial line reads.


This is another FAQ; i believe the solution in the FAQ contains misleading
code.  Any volunteers to update that?


-- 
muppet <scott at asofyet dot org>




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]