Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > running external process?

Reply
Thread Tools

running external process?

 
 
Ferenc Engard
Guest
Posts: n/a
 
      01-18-2004
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


 
Reply With Quote
 
 
 
 
Wesley J Landaker
Guest
Posts: n/a
 
      01-18-2004
--Boundary-02=_WWqCA6VXupBxfHq
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Description: signed data
Content-Disposition: inline

On Sunday 18 January 2004 5:39 am, Ferenc Engard wrote:
> 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 -
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)

iD8DBQBACqWW8KmKTEzW49IRAnIkAJ0Vd2dbU6GPjP0O42Jlms GNTF9W7gCfchXd
OcpyAmJSdkzdgbIEsG4jdsM=
=YLkY
-----END PGP SIGNATURE-----

--Boundary-02=_WWqCA6VXupBxfHq--


 
Reply With Quote
 
 
 
 
Ara.T.Howard
Guest
Posts: n/a
 
      01-18-2004
On Sun, 18 Jan 2004, Ferenc Engard wrote:

> Date: Sun, 18 Jan 2004 21:39:41 +0900
> From: Ferenc Engard <>
> 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'
================================================== =============================

 
Reply With Quote
 
Wesley J Landaker
Guest
Posts: n/a
 
      01-18-2004
--Boundary-02=_gZqCAPrbmqy6PrH
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Description: signed data
Content-Disposition: inline

On Sunday 18 January 2004 8:26 am, Wesley J Landaker wrote:
> On Sunday 18 January 2004 5:39 am, Ferenc Engard wrote:
> > 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
> }


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 -
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)

iD8DBQBACqZg8KmKTEzW49IRAig3AJ47xcktxRif46Koj5jfze TPa2rUDACfTLNr
OrQEmw9R0/vWZq8DcaAS9DM=
=Xy10
-----END PGP SIGNATURE-----

--Boundary-02=_gZqCAPrbmqy6PrH--


 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      01-18-2004

"Ferenc Engard" <> schrieb im Newsbeitrag
news:...
> 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


 
Reply With Quote
 
Ferenc Engard
Guest
Posts: n/a
 
      01-18-2004
> > 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


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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Create references to external scipt files from within an external script file Mellow Crow Javascript 6 11-04-2005 01:16 PM
Running External Programs through ASP.NET =?Utf-8?B?TWF0dA==?= ASP .Net 2 05-13-2005 01:08 PM
unresolved external symbol/using an external dll Scott Allen C++ 8 05-02-2004 06:11 PM
Running External App Possible? Gene Ariani ASP .Net 0 01-15-2004 05:18 AM
Running External Applications in ASP.NET Edward ASP .Net 2 11-13-2003 03:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57