[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Sleep in a multithreaded environment

Ema Fuma

6/30/2008 9:07:00 AM

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
--
Posted via http://www.ruby-....

5 Answers

Zhukov Pavel

6/30/2008 9:35:00 AM

0

On Mon, Jun 30, 2008 at 1:07 PM, Me Me <emanuelef@tiscali.it> wrote:
> 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
> --
> Posted via http://www.ruby-....
>
>

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

Ema Fuma

6/30/2008 9:46:00 AM

0

Zhukov Pavel wrote:
> On Mon, Jun 30, 2008 at 1:07 PM, Me Me <emanuelef@tiscali.it> wrote:
>> Thanks a lot in advance
>> end
>> k = n
>> end
>> --
>> Posted via http://www.ruby-....
>>
>>
>
> 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
--
Posted via http://www.ruby-....

Zhukov Pavel

6/30/2008 10:08:00 AM

0

On Mon, Jun 30, 2008 at 1:45 PM, Me Me <emanuelef@tiscali.it> wrote:
> Zhukov Pavel wrote:
>> On Mon, Jun 30, 2008 at 1:07 PM, Me Me <emanuelef@tiscali.it> wrote:
>>> Thanks a lot in advance
>>> end
>>> k = n
>>> end
>>> --
>>> Posted via http://www.ruby-....
>>>
>>>
>>
>> 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
> --
> Posted via http://www.ruby-....
>
>

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

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

Ema Fuma

6/30/2008 10:54:00 AM

0

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.




--
Posted via http://www.ruby-....

Ema Fuma

6/30/2008 11:14:00 AM

0

Me Me wrote:
> 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
--
Posted via http://www.ruby-....