Timeout question

Hi all,

I iterate an array. During jumping from one element to the next one I
need to get user input from the console. 1) If not input after 5 seconds
I have to jump to the next element. 2)If there is an input within 5
seconds I go for processing the input and then jump to the next
element.

I don’t know how to implement the code for question 1. I wonder if
anyone there has a good idea.

Thanks,

Li

On Sun, Oct 5, 2008 at 8:36 PM, Li Chen [email protected] wrote:

I iterate an array. During jumping from one element to the next one I
need to get user input from the console. 1) If not input after 5 seconds
I have to jump to the next element. 2)If there is an input within 5
seconds I go for processing the input and then jump to the next
element.

how about Timeout ?
pardon this crude example,

botp@jedi-hopeful:~$ cat test.rb

require ‘timeout’
a=(1…10).to_a

STDOUT.sync=true

a.each do |e|
print "> "

begin
Timeout.timeout(5) do
x=gets
puts “you entered: #{x}”
end
rescue Timeout::Error
end

puts e
end

botp@jedi-hopeful:~$ ruby -v test.rb

ruby 1.8.7 (2008-08-08 patchlevel 71) [i686-linux]

1
2
hello
you entered: hello
3
4
5
world
you entered: world
6
7
8
9
10

botp@jedi-hopeful:~$

Hi,

I ran the script(script a) in irb mode and I find it never jumps to next
element without input from outside/console, which is not what I want.
Here is my requirement for the timeout: 1) iterate an array 2)when
jumping from one element to the next wait for 5 seconds for the input.
If no input after 5 seconds then jump to the next element. If there is
an input then process the input. After that also jump to the next
element.

Also I ran two very short scripts (script b and script c) after reading
doc for Timeout. I find that one works and other doesn’t work: without
input from the console the timeout does not work.

Any idea?

Thanks,

Li

####script a: without input the array never move forward ####

C:\Users\Alex>irb
irb(main):001:0> require ‘timeout’
=> false
irb(main):002:0> a=(1…10).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
irb(main):003:0>
irb(main):004:0* STDOUT.sync=true
=> true
irb(main):005:0>
irb(main):006:0* a.each do |e|
irb(main):007:1* print "> "
irb(main):008:1>
irb(main):009:1* begin
irb(main):010:2* Timeout.timeout(5) do
irb(main):011:3* x=gets
irb(main):012:3> puts “you entered: #{x}”
irb(main):013:3> end
irb(main):014:2> rescue Timeout::Error
irb(main):015:2> end
irb(main):016:1>
irb(main):017:1* puts e
irb(main):018:1> end

###script b doesn’t work#####

C:\Users\Alex>irb
irb(main):001:0> require ‘timeout’
=> false
irb(main):002:0> Timeout.timeout(10){gets}

###script c works by raising an error###

C:\Users\Alex>irb
irb(main):001:0> require ‘timeout’
=> false
irb(main):002:0>
irb(main):003:0* Timeout.timeout(10){sleep}
c:/ruby/lib/ruby/1.8/timeout.rb:54:in irb_binding': execution expired (Timeout::Error) from c:/ruby/lib/ruby/1.8/timeout.rb:56:intimeout’
from (irb):3:in irb_binding' from c:/ruby/lib/ruby/1.8/irb/workspace.rb:52:inirb_binding’
from c:/ruby/lib/ruby/1.8/irb/workspace.rb:52

El Domingo, 5 de Octubre de 2008, Li Chen escribió:

Hi all,

I iterate an array. During jumping from one element to the next one I
need to get user input from the console. 1) If not input after 5 seconds
I have to jump to the next element. 2)If there is an input within 5
seconds I go for processing the input and then jump to the next
element.

I don’t know how to implement the code for question 1. I wonder if
anyone there has a good idea.

This works for me:


ARRAY = %w{A B C D E}

ARRAY.each do |letter|

@t_input = Thread.new do
input = gets
puts “#{letter} - String entered by user: #{input}”
end

@t_timer= Thread.new do
sleep 3
puts “Timeout, going to next element…”
@t_input.terminate
end

@t_input.join
@t_timer.terminate

end

Iñaki Baz C. wrote:

El Domingo, 5 de Octubre de 2008, Li Chen escribió:

Hi all,

I iterate an array. During jumping from one element to the next one I
need to get user input from the console. 1) If not input after 5 seconds
I have to jump to the next element. 2)If there is an input within 5
seconds I go for processing the input and then jump to the next
element.

I don’t know how to implement the code for question 1. I wonder if
anyone there has a good idea.

This works for me:


ARRAY = %w{A B C D E}

ARRAY.each do |letter|

@t_input = Thread.new do
input = gets
puts “#{letter} - String entered by user: #{input}”
end

@t_timer= Thread.new do
sleep 3
puts “Timeout, going to next element…”
@t_input.terminate
end

@t_input.join
@t_timer.terminate

end

It doesn’t work for me. The timeout hangs out for ever if no input is
available.

Li

From: Li Chen [mailto:[email protected]]
#…

###script b doesn’t work#####

C:\Users\Alex>irb

irb(main):001:0> require ‘timeout’

=> false

irb(main):002:0> Timeout.timeout(10){gets}

i’m afraid you’ll have no luck in windows (yet)… i think the ruby
windows team (win32utils) is working something like it… not sure
though…

On Oct 5, 2008, at 7:27 PM, Peña, Botp wrote:

though…

http://codeforpeople.com/lib/ruby/terminator/terminator-0.4.2/README

gem install terminator

a @ http://codeforpeople.com/

From: ara.t.howard [mailto:[email protected]]
#…

http://codeforpeople.com/lib/ruby/terminator/terminator-0.4.2/README

am sorry, ara, but gets still blocks… tested using terminator 0.4.4

kind regards -botp

On Oct 5, 2008, at 9:24 PM, Peña, Botp wrote:

From: ara.t.howard [mailto:[email protected]]
#…

http://codeforpeople.com/lib/ruby/terminator/terminator-0.4.2/README

am sorry, ara, but gets still blocks… tested using terminator 0.4.4

hrm. try 0.4.2 and let me know will ya? no windows box handy…

cheers.

a @ http://codeforpeople.com/

Ara Howard wrote:

On Oct 5, 2008, at 9:24 PM, Peñ¡¬ Botp wrote:

From: ara.t.howard [mailto:[email protected]]
#…

http://codeforpeople.com/lib/ruby/terminator/terminator-0.4.2/README

am sorry, ara, but gets still blocks… tested using terminator 0.4.4

hrm. try 0.4.2 and let me know will ya? no windows box handy…

cheers.

a @ http://codeforpeople.com/

No luck for 0.4.4 version, either.

Li