[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

timeout question

Li Chen

10/5/2008 12:36:00 PM

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

9 Answers

botp

10/5/2008 2:07:00 PM

0

On Sun, Oct 5, 2008 at 8:36 PM, Li Chen <chen_li3@yahoo.com> 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:~$

Li Chen

10/5/2008 7:03:00 PM

0

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



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

Iñaki Baz Castillo

10/5/2008 7:21:00 PM

0

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

Li Chen

10/6/2008 12:22:00 AM

0

Iñaki Baz Castillo 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
--
Posted via http://www.ruby-....

Peña, Botp

10/6/2008 1:27:00 AM

0

RnJvbTogTGkgQ2hlbiBbbWFpbHRvOmNoZW5fbGkzQHlhaG9vLmNvbV0gDQojLi4NCiMgIyMjc2Ny
aXB0IGIgZG9lc24ndCB3b3JrIyMjIyMNCiMgQzpcVXNlcnNcQWxleD5pcmINCiMgaXJiKG1haW4p
OjAwMTowPiByZXF1aXJlICd0aW1lb3V0Jw0KIyA9PiBmYWxzZQ0KIyBpcmIobWFpbik6MDAyOjA+
IFRpbWVvdXQudGltZW91dCgxMCl7Z2V0c30NCg0KaSdtIGFmcmFpZCB5b3UnbGwgaGF2ZSBubyBs
dWNrIGluIHdpbmRvd3MgKHlldCkuLi4gaSB0aGluayB0aGUgcnVieSB3aW5kb3dzIHRlYW0gKHdp
bjMydXRpbHMpIGlzIHdvcmtpbmcgc29tZXRoaW5nIGxpa2UgaXQuLi4gbm90IHN1cmUgdGhvdWdo
Li4NCg0KDQo=

ara.t.howard

10/6/2008 2:13:00 AM

0


On Oct 5, 2008, at 7:27 PM, Pe=F1a, Botp wrote:

> From: Li Chen [mailto:chen_li3@yahoo.com]
> #..
> # ###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://codeforp...lib/ruby/terminator/terminator-0....

gem install terminator


a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being =20
better. simply reflect on that.
h.h. the 14th dalai lama




Peña, Botp

10/6/2008 3:25:00 AM

0

From: ara.t.howard [mailto:ara.t.howard@gmail.com]=20
#..
# http://codeforpeople.com/lib/ruby/terminator/terminator-0....

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

kind regards -botp

ara.t.howard

10/6/2008 3:50:00 AM

0


On Oct 5, 2008, at 9:24 PM, Pe=F1a, Botp wrote:

> From: ara.t.howard [mailto:ara.t.howard@gmail.com]
> #..
> # http://codeforp...lib/ruby/terminator/terminator-0....
>
> 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://codeforp...
--
we can deny everything, except that we have the possibility of being =20
better. simply reflect on that.
h.h. the 14th dalai lama




Li Chen

10/6/2008 2:05:00 PM

0

Ara Howard wrote:
> On Oct 5, 2008, at 9:24 PM, Pe񡬠Botp wrote:
>
>> From: ara.t.howard [mailto:ara.t.howard@gmail.com]
>> #..
>> # http://codeforp...lib/ruby/terminator/terminator-0....
>>
>> 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://codeforp...

No luck for 0.4.4 version, either.

Li

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