running external process?

F

Ferenc Engard

Hi all,

Is there a simple way to spawn an external program, feed its stdin, and
get its stdout?

The problem with popen is, if I want to feed a few MB's of input to it,
then it hangs (I suspect that its stdout IO buffer is full) before I
could read out its stdout on the next line. So, the following do not
work:

io=IO.popen("externalfilter")
io.write(verybigstring)
result=io.read

Thanks,
Ferenc
 
W

Wesley J Landaker

--Boundary-02=_WWqCA6VXupBxfHq
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Description: signed data
Content-Disposition: inline

Hi all,

Is there a simple way to spawn an external program, feed its stdin,
and get its stdout?

The problem with popen is, if I want to feed a few MB's of input to
it, then it hangs (I suspect that its stdout IO buffer is full)
before I could read out its stdout on the next line. So, the
following do not work:

io=3DIO.popen("externalfilter")
io.write(verybigstring)
result=3Dio.read

require 'open3'
Open3.popen3("externalfilter") { |sin,sout,serr|
sin.write(verybigstring)
result =3D sout.read
}

=2D-=20
Wesley J. Landaker - (e-mail address removed)
OpenPGP FP: 4135 2A3B 4726 ACC5 9094 0097 F0A9 8A4C 4CD6 E3D2


--Boundary-02=_WWqCA6VXupBxfHq
Content-Type: application/pgp-signature
Content-Description: signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQBACqWW8KmKTEzW49IRAnIkAJ0Vd2dbU6GPjP0O42JlmsGNTF9W7gCfchXd
OcpyAmJSdkzdgbIEsG4jdsM=
=YLkY
-----END PGP SIGNATURE-----

--Boundary-02=_WWqCA6VXupBxfHq--
 
A

Ara.T.Howard

Date: Sun, 18 Jan 2004 21:39:41 +0900
From: Ferenc Engard <[email protected]>
Newsgroups: comp.lang.ruby
Subject: running external process?

Hi all,

Is there a simple way to spawn an external program, feed its stdin, and
get its stdout?

The problem with popen is, if I want to feed a few MB's of input to it,
then it hangs (I suspect that its stdout IO buffer is full) before I
could read out its stdout on the next line. So, the following do not
work:

io=IO.popen("externalfilter")
io.write(verybigstring)
io.close # the program is still reading it's stding here
result=io.read

Thanks,
Ferenc

-a
--

ATTN: please update your address books with address below!

===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
| STP :: http://www.ngdc.noaa.gov/stp/
| NGDC :: http://www.ngdc.noaa.gov/
| NESDIS :: http://www.nesdis.noaa.gov/
| NOAA :: http://www.noaa.gov/
| US DOC :: http://www.commerce.gov/
|
| The difference between art and science is that science is what we
| understand well enough to explain to a computer.
| Art is everything else.
| -- Donald Knuth, "Discover"
|
| /bin/sh -c 'for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done'
===============================================================================
 
W

Wesley J Landaker

--Boundary-02=_gZqCAPrbmqy6PrH
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Description: signed data
Content-Disposition: inline

require 'open3'
Open3.popen3("externalfilter") { |sin,sout,serr|
sin.write(verybigstring)
result =3D sout.read
}

Or (should have mentioned this in my first e-mail) if that still hangs,=20
you might try using Threads:

require 'open3'
Open3.popen3("externalfilter") { |sin,sout,serr|
threads =3D []
threads << Thread.new {
sin.write(verybigstring)
}
threads << Thread.new {
result =3D sout.read
}
threads.each {|t| t.join }
}

=2D-=20
Wesley J. Landaker - (e-mail address removed)
OpenPGP FP: 4135 2A3B 4726 ACC5 9094 0097 F0A9 8A4C 4CD6 E3D2


--Boundary-02=_gZqCAPrbmqy6PrH
Content-Type: application/pgp-signature
Content-Description: signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQBACqZg8KmKTEzW49IRAig3AJ47xcktxRif46Koj5jfzeTPa2rUDACfTLNr
OrQEmw9R0/vWZq8DcaAS9DM=
=Xy10
-----END PGP SIGNATURE-----

--Boundary-02=_gZqCAPrbmqy6PrH--
 
R

Robert Klemme

Ferenc Engard said:
Hi all,

Is there a simple way to spawn an external program, feed its stdin, and
get its stdout?

The problem with popen is, if I want to feed a few MB's of input to it,
then it hangs (I suspect that its stdout IO buffer is full) before I
could read out its stdout on the next line. So, the following do not
work:

io=IO.popen("externalfilter")
io.write(verybigstring)
result=io.read

You need at least two threads or non blocking IO. How about:

IO.popen( "cat", "w+" ) do |io|
r = Thread.new(io) do |reader|
while ( line = reader.gets )
line.chomp!
$stdout.puts line
end
end

(1..100000).each {|i| io.puts i}
r.join
end

Regards

robert
 
F

Ferenc Engard

The problem with popen is, if I want to feed a few MB's of input to it,
You need at least two threads or non blocking IO. How about:

IO.popen( "cat", "w+" ) do |io|
r = Thread.new(io) do |reader|
while ( line = reader.gets )
line.chomp!
$stdout.puts line
end
end

(1..100000).each {|i| io.puts i}
r.join
end

Thank you, this is what I need. Since I program in ruby, a 8-9 rows of
code seems too difficult to write by myself. :-/

Ferenc
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top