Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Concurrency with IPC::Run3 ?

by LanX (Sage)
on Jul 03, 2022 at 22:10 UTC ( #11145257=perlquestion: print w/replies, xml ) Need Help??

LanX has asked for the wisdom of the Perl Monks concerning the following question:

Pardon my ignorance, but I have trouble understand the docs of IPC::run3

How do I implement the following, including catching stdout and stderr with run3?

the docs claim

    compared to system(), qx'', open "...|", open "|..."

    ... BUT ...

    Note that this form of redirecting the child's I/O doesn't imply any form of concurrency between parent and child - run3()'s method of operation is the same no matter which form of redirection you specify.

I'm confused, is it even possible to have a bidirectional communication between two simultaneously running processes with run3?

If not what's the appropriate solution?

client.pl
use v5.12; use warnings; use IPC::Run3; my ($cmd, $in, $out, $err); $cmd = 'perl ./server.pl'; open my $fh_in,"|-", $cmd; #my @in; #run3($cmd, \@in); sub out { $fh_in->say(@_); } my $doit; for my $try (2,3,2) { $doit = "$;" x $try; out($doit); out($_) for 1..$try; out($doit); } out("EOF");

server.pl
use v5.12; use warnings; STDOUT->autoflush; my $doit; my $input; while ( my $line = <STDIN> ) { unless ( defined $doit ){ $doit = $line; last if $doit eq "EOF"; next; } if ($line eq "$doit") { doit(); undef $doit; } else { $input .= $line; } } say "exit by $doit"; exit; sub doit { print "((( STDOUT ::: $doit"; print $input; print "))) STDOUT ::: $doit"; #warn "STDERR:",$input," "; $input =""; }

output
((( STDOUT :::  1 2 ))) STDOUT :::  ((( STDOUT :::  1 2 3 ))) STDOUT :::  ((( STDOUT :::  1 2 ))) STDOUT :::  exit by EOF

update

found this

Bidirectional Communication with Another Process

doesn't seem to be overly portable and I doubt run3 is the solution...

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re: Concurrency with IPC::Run3 ?
by haukex (Archbishop) on Jul 04, 2022 at 06:31 UTC
    is it even possible to have a bidirectional communication between two simultaneously running processes with run3?

    No, IPC::Run3 doesn't support interactive communications with the subprocesses. IIRC, it does most of its I/O redirections via files, since that's the most portable.

    If not what's the appropriate solution?

    I've had success with IPC::Run. According to CPAN Testers it's not as portable, but so far I haven't had problems with it.

      Thanks a lot, that helped.

      For the time being I will "bake smaller buns" and to set my sights lower and start with a prove of concept with one integrated application for this, most likely with Tk.

      If that works I'll split it up into a parent-child structure and try to find the most portable interface.

      The parent is basically for testing the child-server, before trying to plug it into an IDE.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11145257]
Approved by kcott
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2022-08-13 22:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?