timeout question

L

Li Chen

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
 
B

botp

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

botp@jedi-hopeful:~$
 
L

Li Chen

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:in `timeout'
from (irb):3:in `irb_binding'
from c:/ruby/lib/ruby/1.8/irb/workspace.rb:52:in `irb_binding'
from c:/ruby/lib/ruby/1.8/irb/workspace.rb:52
 
I

Iñaki Baz Castillo

El Domingo, 5 de Octubre de 2008, Li Chen escribi=C3=B3:
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:

=2D------------
ARRAY =3D %w{A B C D E}

ARRAY.each do |letter|
=09
@t_input =3D Thread.new do
input =3D gets
puts "#{letter} - String entered by user: #{input}"
end
=09
@t_timer=3D Thread.new do
sleep 3
puts "Timeout, going to next element..."
@t_input.terminate
end
=09
@t_input.join
@t_timer.terminate

end
=2D------------


=2D-=20
I=C3=B1aki Baz Castillo
 
L

Li Chen

Iñaki Baz Castillo said:
El Domingo, 5 de Octubre de 2008, Li Chen escribió:

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
 
P

Peña, Botp

RnJvbTogTGkgQ2hlbiBbbWFpbHRvOmNoZW5fbGkzQHlhaG9vLmNvbV0gDQojLi4NCiMgIyMjc2Ny
aXB0IGIgZG9lc24ndCB3b3JrIyMjIyMNCiMgQzpcVXNlcnNcQWxleD5pcmINCiMgaXJiKG1haW4p
OjAwMTowPiByZXF1aXJlICd0aW1lb3V0Jw0KIyA9PiBmYWxzZQ0KIyBpcmIobWFpbik6MDAyOjA+
IFRpbWVvdXQudGltZW91dCgxMCl7Z2V0c30NCg0KaSdtIGFmcmFpZCB5b3UnbGwgaGF2ZSBubyBs
dWNrIGluIHdpbmRvd3MgKHlldCkuLi4gaSB0aGluayB0aGUgcnVieSB3aW5kb3dzIHRlYW0gKHdp
bjMydXRpbHMpIGlzIHdvcmtpbmcgc29tZXRoaW5nIGxpa2UgaXQuLi4gbm90IHN1cmUgdGhvdWdo
Li4NCg0KDQo=
 
A

ara.t.howard

From: Li Chen [mailto:[email protected]]
#..
# ###script b doesn't work#####
# C:\Users\Alex>irb
# irb(main):001:0> require 'timeout'
# =3D> false
# irb(main):002:0> Timeout.timeout(10){gets}

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


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

gem install terminator


a @ http://codeforpeople.com/
 

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

Forum statistics

Threads
474,200
Messages
2,571,046
Members
47,646
Latest member
xayaci5906

Latest Threads

Top