I have a program which is calling another program and processing the child’s output, ie:
my $pid = open($handle, "$commandPath $options |");
Now I’ve tried a couple different ways to read from the handle without blocking with little or no success.
I found related questions:
- perl-win32-how-to-do-a-non-blocking-read-of-a-filehandle-from-another-process
- why-does-my-perl-sysread-block-when-reading-from-a-socket
But they suffer from the problems:
ioctlconsistently crashes perlsysreadblocks on 0 bytes (a common occurrence)
I’m not sure how to go about solving this problem.
Pipes are not as functional on Windows as they are on Unix-y systems. You can’t use the 4-argument
selecton them and the default capacity is miniscule.You are better off trying a socket or file based workaround.
Now you have a couple more cans of worms to deal with — running
waitpidperiodically to check when the background process has stopped creating output, callingseek $handle,0,1to clear the eof condition after you read from$handle, cleaning up the temporary file, but it works.I have written the
Forks::Supermodule to deal with issues like this (and many others). For this problem you would use it like