trouble waiting X minutes

T

Tee Dubb

I am making a constant network connection that is feeding me
information, i want to take that information and place into nested /
two-dimensional array for 1 minute, below is the code that's not
working.

class Test

def initialize
@junk = Array.new
@opensocket = TCPSocket.new('localhost', 8080)
read
end

def read
while @lines = @opensocket.gets
FasterCSV.parse(@lines) do |@row|
pass
end
end
end

def pass
@junk << @row
sleep(60)
p @junk
end
end

enable = Test.new

So after I sleep for 1 minute you would think the @junk two dimensional
array has filled up but it hasn't, what's the best way to solve this
problem?
 
B

Ben Bleything

I am making a constant network connection that is feeding me
information, i want to take that information and place into nested /
two-dimensional array for 1 minute, below is the code that's not
working.
[snip]

So after I sleep for 1 minute you would think the @junk two = dimensional
array has filled up but it hasn't, what's the best way to solve this
problem?

Well, *I* wouldn't think that would be the case... Sleep suspends the =
execution of your program until the timer expires, so nothing else is =
happening during that time.

There are lots of ways that you could do what you want... Perhaps the =
"simplest" way would be to capture the time in a variable before you =
start, and check it after every row. If a minute has gone by, stop. =
However, that won't be terribly accurate and you won't guarantee exactly =
one minute.

Another option would be to start another thread to manage the timing, =
but I'm not sure right off the top of my head what that would look like.

A third option might be to use the Timeout class, but that is sort of a =
hack. You should read up on these options and pick one that works for =
you.

ri Thread
ri Timeout

Ben=
 
B

Brian Candler

Tee said:
below is the code that's not
working.

Your program doesn't have any threads, so it will execute in lock-step.
That is, when you enter pass, you will sleep for 60 seconds there,
before returning.

Try adding some puts statements to see what's going on, e.g.
class Test

def initialize

puts "start initialize"
@junk = Array.new
@opensocket = TCPSocket.new('localhost', 8080)

puts "socket open"

puts "read has returned, done initialize"
end

def read

puts "start read"
while @lines = @opensocket.gets
FasterCSV.parse(@lines) do |@row|

puts "Read line #{@row.inspect}, going to pass"

puts "pass has returned"

puts "done read"
end

def pass

puts "starting pass"
@junk << @row

puts "sleeping for 60..."
sleep(60)
p @junk
end
end

enable = Test.new

You could alternatively try to use a debugger to single-step the
program, but generally I find the hassle of learning to drive the
debugger to be higher than putting the debug statements in.
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top