open() and pipes
Uri Guttman
uri at stemsystems.com
Tue Aug 1 17:43:19 BST 2006
>>>>> "DC" == David Cantrell <david at cantrell.org.uk> writes:
DC> open(FOO), '|myprogram outputfile=foo.tmp');
DC> print FOO 'munge this';
DC> close(FOO);
DC> # at this point some naughty chap replaces foo.tmp just to piss me off
DC> open(FOORESULTS, 'foo.tmp');
DC> my $munged = <FOORESULTS>;
DC> close(FOORESULTS);
DC> unlink 'foo.tmp';
DC> with this ...
DC> open(FOO, '|myprogram|');
DC> print FOO, 'munge this';
DC> my $munged = <FOO>;
DC> close(FOO);
stem can do that easily. it has a mode to send data to a child process
and then get sent back all the output in one message when the process
closes/dies. it isn't hard to do with ipc::open2 either. just do your
hack with that module and send it all the data and slurp the results
(hey, you can use file::slurp for both directions!). but one issue is
having the subprocess detect EOF on its stdin so it can close stdout or
die so then you can detect eof on reading from it. this can be done with
closing the writing handle from the ipc::open2 or by sending a close
command (in some format the subproc can parse) so it will then close/die
cleanly.
the only caveat with the above ipc2::open hack is what you have noted
before. if you send so much data that you force the subproc to send tons
back before you read it, the subproc may block and you could have i/o
deadlock. this is also where stem wins out as it uses async i/o and
can't ever block like that. poe can do it to of course as it isn't that
hard. hell, it isn't hard to write it using event.pm by itself. you just
need to convert to event loop type coding and not sync. async is much
better for ipc than sync in so many ways but too few learn or master it
and too many cpan modules are written in sync style since is is easy but
limiting.
gisle and i met at oscon last week and are going to collaborate on
converting LWP to become async internally so it can be cleanly used with
event loop apps. it will get a set of its own callbacks and internal
event loop so you can use the current sync API as is but there will be
hooks to set those callbacks to your own which will be in your code and
use an external event loop. he wants that so he can use lwp with tk and
i want it for stem. lwp::parallel sucks dead donkeys and uses a closed
event loop so you can't do anything BUT lwp::parallel let alone use a
different event loop.
uri
--
Uri Guttman ------ uri at stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
More information about the london.pm
mailing list