exec and pipe

I

ientu

Hi All,

I have been coding-testing-googling all day long but I couldn't find a
solution for this.
I need to create a pipe between some ruby code and a perl script. The
perl script will be the writer: the script prints on STDOUT lines of
information at a random pace.
These lines are to be processed in ruby in real time, as soon as they
enter the pipe, without waiting for the termination of the perl script.

Another requirement is that the perl script must be invoked from a ruby
class.

I have tried with the following:

readme, writeme = IO.pipe
pid = fork {
$stdout = writeme
readme.close
exec('perl some_script.pl')
}
# Process.waitpid(pid,0)
# do not wait for the process to terminate
# read output in real time

writeme.close
while readme.gets do
puts 'processing: ' + $_
...
end




This doesn't work, apparently when I run 'exec' STDOUT is reopened and
I get the output from the perl script on screen.

What am I doing wrong?
Is this the right approach?


Many thanks
/giulio
 
H

Hugh Sasse

Hi All,

I have been coding-testing-googling all day long but I couldn't find a
solution for this.
I need to create a pipe between some ruby code and a perl script. The
perl script will be the writer: the script prints on STDOUT lines of
information at a random pace.
These lines are to be processed in ruby in real time, as soon as they
enter the pipe, without waiting for the termination of the perl script.

Another requirement is that the perl script must be invoked from a ruby
class.

I have tried with the following:

readme, writeme = IO.pipe
pid = fork {
$stdout = writeme
readme.close
exec('perl some_script.pl')

I think you want IO.popen for this. Two examples that worked for
me:
http://www.eng.cse.dmu.ac.uk/~hgs/ruby/html_spell.rb
http://www.eng.cse.dmu.ac.uk/~hgs/ruby/top.rb

Hugh
 
I

ientu

I think IO.popen waits for the process to terminate before putting any
data into the pipe. This doesn't work for me, as I have to process the
output in real time.

thanks
..g
 
A

Ara.T.Howard

I think IO.popen waits for the process to terminate before putting any
data into the pipe. This doesn't work for me, as I have to process the
output in real time.

that's what popen is for:

harp:~ > cat a.rb
STDOUT.sync = true and loop{ sleep 0.42 and puts Time::now }

harp:~ > cat b.rb
IO::popen('ruby a.rb'){|pipe| loop{ print pipe.gets }}

harp:~ > ruby b.rb
Thu Oct 13 09:58:03 MDT 2005
Thu Oct 13 09:58:03 MDT 2005
Thu Oct 13 09:58:04 MDT 2005
Thu Oct 13 09:58:04 MDT 2005
Thu Oct 13 09:58:04 MDT 2005
Thu Oct 13 09:58:05 MDT 2005
Thu Oct 13 09:58:05 MDT 2005
Thu Oct 13 09:58:06 MDT 2005
Thu Oct 13 09:58:06 MDT 2005
Thu Oct 13 09:58:07 MDT 2005
Thu Oct 13 09:58:07 MDT 2005
Thu Oct 13 09:58:07 MDT 2005
Thu Oct 13 09:58:08 MDT 2005
Thu Oct 13 09:58:08 MDT 2005
Thu Oct 13 09:58:09 MDT 2005
...
...
...

perhaps your perl code is buffering output. make sure the stdout of the perl
process is not fully buffered. programs are, by default, fully buffered when
run into a pipe.


you can 'fix' broken code by creating a wrapper that does

#! /usr/bin/env ruby
STDOUT.sync = true
exec(ARGV.join(' '))

and running all code, including perl code, under this wrapper. so

IO::popen('wrapper a.pl'){|pipe| loop{ print pipe.gets }}


hth.

-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| Your life dwells amoung the causes of death
| Like a lamp standing in a strong breeze. --Nagarjuna
===============================================================================
 
I

ientu

thanks Ara!

probably I should have posted this on a perl newsgroup ....
adding
autoflush STDOUT 1
to my perl script solved the problem!


thanks gain
..g
 
A

Ara.T.Howard

Hi,

At Fri, 14 Oct 2005 01:21:55 +0900,
Ara.T.Howard wrote in [ruby-talk:160438]:
you can 'fix' broken code by creating a wrapper that does

#! /usr/bin/env ruby
STDOUT.sync = true
exec(ARGV.join(' '))

IO#sync flag can't affect other processes.

sheesh. you're right of course... how does using a pty affect this?

-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| Your life dwells amoung the causes of death
| Like a lamp standing in a strong breeze. --Nagarjuna
===============================================================================
 
N

nobuyoshi nakada

Hi,

At Fri, 14 Oct 2005 11:57:27 +0900,
Ara.T.Howard wrote in [ruby-talk:160549]:
sheesh. you're right of course... how does using a pty affect this?

$ ruby -e 'IO.popen(%[ruby -e "p STDOUT.tty?"]) {|i| p i.read}'
"false\n"

$ ruby -rpty -e 'PTY.spawn(%[ruby -e "p STDOUT.tty?"]) {|i,o| p i.read}'
"true\r\n"
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top