popen, open3 problems

P

Peter Schrammel

Hi,

I think this problem is surely solved but hven't found anything on the net:

I get two programs:
1. a ruby (master)
2. a perl (slave)

I want the master to set commands to the slave and wait for the answer
(repeatedly). So a conversation could look like this:

m: set_var
m: v=8
s: set
m: get_var
m: v
s: 8
....

The problem is that ruby refuses to write to stdin of the slave until I
close the pipe. But I want to use the pipe repeatedly. Did I miss
something about IPC?

Thanks


My curent progs look like this:


require 'fcntl'
require 'open3'

pin, pout, perr = Open3.popen3('perl test.pl')
pin.fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK)
pout.fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK)
perr.fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK)

pin.syswrite "set_var\n"
pin.syswrite "v=8\n"
#pin.close_write #<----------don't want to do this but have to!
result=pout.readline
puts result
#pin.close
pout.close
perr.close

---------------------------------------------------------------------------
perl slave:

while ($line = <STDIN>) { #first line is the command
chomp($line);
if ($line eq "set_var") { &set_var;}
elsif ($line eq "quit") { exit 0;}
}

sub set_var {
$var=<STDIN>; #second...lines are the params; get the var
chomp($var);
print "set."; #return a confirmation
}
 
B

Bernd Haug

Peter Schrammel said:
The problem is that ruby refuses to write to stdin of the slave until I
close the pipe. But I want to use the pipe repeatedly. Did I miss
something about IPC?

Are you sure it's that Ruby didn't send the data, and not that perl
didn't answer yet?

I.e., have you tried setting $| on the perl side?
http://perl.plover.com/FAQs/Buffering.html

I love perl, it's an awesome language, but unfortunately there are many
magic nooks and crannies.

lg, Bernd
 
A

ara.t.howard

Hi,

I think this problem is surely solved but hven't found anything on the net:

I get two programs:
1. a ruby (master)
2. a perl (slave)

I want the master to set commands to the slave and wait for the answer
(repeatedly). So a conversation could look like this:

m: set_var
m: v=8
s: set
m: get_var
m: v
s: 8
...

The problem is that ruby refuses to write to stdin of the slave until I
close the pipe. But I want to use the pipe repeatedly. Did I miss
something about IPC?


man stdio
/buffer
My curent progs look like this:


require 'fcntl'
require 'open3'

harp:~ > cat a.rb
require 'open3'

Open3.popen3 "perl a.pl" do |i,o,e|
i.puts 'set_var'
i.puts 'v=8'
i.flush # <<---

puts o.gets
end


harp:~ > cat a.pl

$| = 1; # <<---

while ($line = <STDIN>) {
chomp($line);
if ($line eq "set_var") { &set_var;}
elsif ($line eq "quit") { exit 0;}
}

sub set_var { $var=<STDIN>; chomp($var); print "set.$var\n"; }


harp:~ > ruby a.rb
set.v=8


-a
 
P

Peter Schrammel

i.puts 'v=8'
i.flush # <<---

ok. i don't need this if I use syswrite (flush flushes only ruby buffers)
puts o.gets
end


harp:~ > cat a.pl

$| = 1; # <<---

yes! that's it. I knew there was some perl-guru-thing about it.

while ($line = <STDIN>) {
chomp($line);
if ($line eq "set_var") { &set_var;}
elsif ($line eq "quit") { exit 0;}
}

sub set_var { $var=<STDIN>; chomp($var); print "set.$var\n"; }


harp:~ > ruby a.rb
set.v=8

thanks very much.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top