Popen, rsh, and File::NONBLOCK

rubyists-

this a bit of *nix question, but, using pipes, how does one either

a) determine if an IO.gets/read etc would block

or

b) use non-blocking io with pipes

this certainly does not work…

~/eg/ruby > irb
irb(main):001:0> cat = IO.popen ‘cat’, File::RDWR | File::NOCTTY | File::NONBLOCK
#IO:0x401fa334
irb(main):002:0> cat.puts ‘foobar’
nil
irb(main):003:0> cat.gets
“foobar\n”
irb(main):004:0> cat.gets
(blocks…)

does popen then not respect the File::NONBLOCK? perhaps this is a limitation
of pipes themselves then…

any suggestion of a means to send/receive commands/output using rsh and ruby?

-a

···

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ara.t.howard@noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
~ > ruby -e ‘p(%.\x2d\x29…intern)’
====================================

Hi,

this a bit of *nix question, but, using pipes, how does one either

a) determine if an IO.gets/read etc would block

io/wait in rough provides IO#ready?.

$ ruby -rio/wait -e ‘cat = IO.popen “cat”, “r+”;cat.puts “foobar”;sleep 0.1;p cat.ready?;p cat.gets;p cat.ready?’
7
“foobar\n”
nil

or

b) use non-blocking io with pipes

Also nonblock.rb is in io/wait.

$ ruby -rio/nonblock -e ‘cat = IO.popen “cat”, “r+”;cat.nonblock=true;cat.puts “foobar”;p cat.gets;p cat.read(1)’
“foobar\n”
-e:1:in `read’: Resource temporarily unavailable (Errno::EAGAIN)
from -e:1

Note that IO#gets waits till a newline even in non-blocking
mode.

this certainly does not work…

~/eg/ruby > irb
irb(main):001:0> cat = IO.popen ‘cat’, File::RDWR | File::NOCTTY | File::NONBLOCK

Only access mode has meanings in the second argument to
IO.popen. NONBLOCK is ignored here.

NOCTTY affects to only terminal device.

does popen then not respect the File::NONBLOCK? perhaps this is a limitation
of pipes themselves then…

Basically, pipe(2) doesn’t take optional flags. IO.popen just
follows it. It would be possible to treat the flag as
special…

···

At Tue, 17 Jun 2003 06:17:25 +0900, ahoward wrote:


Nobu Nakada

io/wait in rough provides IO#ready?.

$ ruby -rio/wait -e ‘cat = IO.popen “cat”, “r+”;cat.puts “foobar”;sleep 0.1;p cat.ready?;p cat.gets;p cat.ready?’
7
“foobar\n”
nil

Also nonblock.rb is in io/wait.

$ ruby -rio/nonblock -e ‘cat = IO.popen “cat”, “r+”;cat.nonblock=true;cat.puts “foobar”;p cat.gets;p cat.read(1)’
“foobar\n”
-e:1:in `read’: Resource temporarily unavailable (Errno::EAGAIN)
from -e:1

very cool - these will be in 1.8?

Note that IO#gets waits till a newline even in non-blocking mode.

i did overlook that…

Only access mode has meanings in the second argument to IO.popen. NONBLOCK
is ignored here.

i figured as much

NOCTTY affects to only terminal device.

if you are spawning an rsh command it is required though…

Basically, pipe(2) doesn’t take optional flags. IO.popen just follows it.
It would be possible to treat the flag as special…

i think it makes sense - but that’s my opinion… of course there would be
issues with fifos then where NONBLOCK does mean something…

IMHO it would be very useful to be able to do something like

stdin, stdout, stderr = Process.new ‘external.sh’

stdin.puts command
puts stdout.gets # should not block
puts stderr.gets # should not block

essentially a means of sending commands to a process, getting back the stdout
and stderr, and being able to do this without blocking on io. is this
sufficiently common to put together an RCR?

-a

···

On Tue, 17 Jun 2003 nobu.nokada@softhome.net wrote:

====================================

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ara.t.howard@noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
~ > ruby -e ‘p(%.\x2d\x29…intern)’
====================================