Subject: | Can't get a forked process to "block" stdin/stdout filehandles |
Date: | Fri, 31 Aug 2018 21:25:21 -0600 (Mountain Daylight Time) |
To: | bug-Forks-Super [...] rt.cpan.org |
From: | "Christian J. Robinson" <heptite [...] gmail.com> |
Under Windows 10, using Strawberry Perl, I am trying to use
Forks::Super to create a sub-process that handles the job of gathering
a small amount of data from a website (via an API key).
The sub-process (child) is supposed to gather that data then print it
to STDOUT, and wait for the parent process to read that data before it
does it all over again.
The parent process only tries to read the data from the child every 20
minutes, and that data is hopefully immediately available.
Unfortunately the whole thing seems to fail to work as expected:
$pid = fork {child_fh => ['in', 'out', 'pipe', 'block'], on_busy => 'fail'};
die "fork failed: $!" unless defined $pid;
if ($pid)
{
$child_fh = $pid->{child_stdout};
$line = <$child_fh>;
print ">>$line<<\n";
} else {
while (1)
{
@aqi = &getAQI;
print STDOUT join(' ', @aqi)."\n";
}
}
The &getAQI subroutine can take up to a minute to complete, and
meanwhile the parent process IMMEDIATELY outputs ">><<" instead of
waiting for the child process to gather the data and send it.
Moreover, the child process seems to rapidly loop instead of waiting
for the parent to gather the data before proceeding to run the data
gathering subroutine again.
I've tried using IO::Select::can_read/can_write on the file handles to
wait for data, but that doesn't work either.
Am I making a fundamental mistake, or have I discovered a bug?
perl -v output:
This is perl 5, version 24, subversion 0 (v5.24.0) built for MSWin32-x64-multi-thread
--
Christian J. Robinson <heptite@gmail.com> -- https://christianrobinson.name/
And on the seventh day, He exited from append mode.