Sleep in a multithreaded environment

M

Me Me

Hi,
I'm trying to control an external process I'm running and quit it in
case it got
stuck.
To do that I have to check that the external exe keeps writing on
stdout, if it doesn't update it for a while I'll quit it.
I came up with the following code (using threads) but the sleep in the
main thread blocks everything. (The same code works under Mac OSX)
Is there something I'm doing wrong?
Or a better way to achieve what I need?
Thanks a lot in advance

n = 0
t1 = Thread.new
{
begin
IO.popen("executable.exe") do |pipe|
pipe.each("\r") do |line|
puts line
n += 1
end
end
end
}

k = 0
while t1.alive?
sleep(10) # DOESN'T SEEM TO WORK ON WINDOWS
if k == n
puts "DEAD"
t1.raise("close")
end
k = n
end
 
Z

Zhukov Pavel

Hi,
I'm trying to control an external process I'm running and quit it in
case it got
stuck.
To do that I have to check that the external exe keeps writing on
stdout, if it doesn't update it for a while I'll quit it.
I came up with the following code (using threads) but the sleep in the
main thread blocks everything. (The same code works under Mac OSX)
Is there something I'm doing wrong?
Or a better way to achieve what I need?
Thanks a lot in advance

n = 0
t1 = Thread.new
{
begin
IO.popen("executable.exe") do |pipe|
pipe.each("\r") do |line|
puts line
n += 1
end
end
end
}

k = 0
while t1.alive?
sleep(10) # DOESN'T SEEM TO WORK ON WINDOWS
if k == n
puts "DEAD"
t1.raise("close")
end
k = n
end

pipe=IO.popen("executable.exe")
io=select([io],nil,nil,10) #10 - timeout
if io==nil
puts "DEAD"
t1.raise("close")
end
 
M

Me Me

Zhukov said:
Thanks a lot in advance
end
k = n
end

pipe=IO.popen("executable.exe")
io=select([io],nil,nil,10) #10 - timeout
if io==nil
puts "DEAD"
t1.raise("close")
end

Hi,
thanks for answering, I tried your code but I get:
test_th.rb:20:in `select': can't convert nil into IO (TypeError)

Besides it's not clear to me what is done by the select.
Do I still need the thread t1 then?
Bye
 
Z

Zhukov Pavel

Zhukov said:
Thanks a lot in advance
end
k = n
end

pipe=IO.popen("executable.exe")
io=select([io],nil,nil,10) #10 - timeout
if io==nil
puts "DEAD"
t1.raise("close")
end

Hi,
thanks for answering, I tried your code but I get:
test_th.rb:20:in `select': can't convert nil into IO (TypeError)

Besides it's not clear to me what is done by the select.
Do I still need the thread t1 then?
Bye

oh, sorry.: io=select([pipe],nil,nil,10)

no, you does't, just add code if you want to catch exeption there
 
M

Me Me

Hi,

I tried this:

pipe=IO.popen("hang.exe")
io=select([pipe],nil,nil,10) #10 - timeout
if io==nil
puts "DEAD"
end

with hang.exe a process that just hangs.
It doesn't seem that the process is launched at all, and it goes on
without waiting.

Basically I just need to parse an executable output line by line and
trigger an exception if I don't get any line after a certain amount of
time.
 
M

Me Me

Me said:
Hi,

I tried this:

pipe=IO.popen("hang.exe")
io=select([pipe],nil,nil,10) #10 - timeout
if io==nil
puts "DEAD"
end

with hang.exe a process that just hangs.
It doesn't seem that the process is launched at all, and it goes on
without waiting.

Actually I checked and the process is running but it became a zombie.
It seems that the select doesn't wait
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top