Shay H. wrote:
Test program:
…
tstamp("> Time’s up! Sending input…")
But that message never appears in the output you showed - and this line
occurs before the puts. So at the moment looks either (a) sleep(5) is
sleeping indefinitely, or (b) the whole thread has terminated with an
exception.
A better test would be a standalone pair of ruby programs, because then
you have something which anyone can reproduce the problem with (*).
Here’s your test program, very slightly modified, with a partner
program.
==> prg1.rb <==
Thread.abort_on_exception = true
require “open3”
command = “ruby prg2.rb”
inline, outline, errline = Open3.popen3(command)
def tstamp(line)
puts("#{Time.now().to_s().split(" “)[3]} #{line}”)
end
Read from stderr, thread stdin input
loop do
tstamp("> Awaiting data…")
line = errline.gets().strip()
tstamp(“Output: #{line}”)
if(line == “0”)
tstamp("> Received the required line.")
Thread.new() do
tstamp("> The thread has been created.")
sleep(5)
tstamp("> Time's up! Sending input...")
inline.puts("EXIT")
tstamp("> The input has been sent.")
end
end
end
==> prg2.rb <==
i = -2
loop do
$stderr.puts i
i += 1
if select([$stdin],nil,nil,2)
line = $stdin.gets.chomp
$stderr.puts “>>> Received #{line} <<<”
end
end
And if I run it under Linux, here’s what I get:
$ ruby prg1.rb
19:58:05 > Awaiting data…
19:58:05 Output: -2
19:58:05 > Awaiting data…
19:58:07 Output: -1
19:58:07 > Awaiting data…
19:58:09 Output: 0
19:58:09 > Received the required line.
19:58:09 > The thread has been created.
19:58:09 > Awaiting data…
19:58:11 Output: 1
19:58:11 > Awaiting data…
19:58:13 Output: 2
19:58:13 > Awaiting data…
19:58:14 > Time’s up! Sending input…
19:58:14 > The input has been sent.
19:58:14 Output: >>> Received EXIT <<<
19:58:14 > Awaiting data…
19:58:14 Output: 3
19:58:14 > Awaiting data…
19:58:16 Output: 4
19:58:16 > Awaiting data…
19:58:18 Output: 5
19:58:18 > Awaiting data…
^Cprg1.rb:13:in gets': Interrupt from prg1.rb:13 from prg1.rb:11:in
loop’
from prg1.rb:11
What happens if you run this under Windows?
Now, this slightly muddies the water because prg2.rb also depends on
being able to select(), as I wanted it to be able to sleep and receive
data on stdin, the same as your main program does. So it would be also
interesting to know what happens if you do “ruby prg2.rb” by itself. It
should print a new number every 2 seconds, and if you type something
onto stdin, it should echo it back as >>> Received … <<<
What I’m trying to say is, if you can make prg2.rb work properly (or
make another of version of prg2 work using threads), then you ought to
be able to make prg1.rb work properly too.
(*) Incidentally: for someone to reproduce this problem, they’ll need to
know exactly what platform you have. You said “Windows Vista” but you
didn’t say which version of Ruby, nor which package (Cygwin? One-Click
Installer? Other?)
Regards,
Brian.