Exits after sleep; ideas?

S

SkyFox

I'm writing a script that retrieves multiple xml files from a remote
server within a loop, and I need to build in a short delay (say, 2
seconds) between requests to avoid overloading the server. I tried to
use the sleep method to do this, but the script exits without executing
any of the code that comes after the sleep call. I searched this group
for similar problems, and I found an earlier thread ("Continuous
running thread" from Nov. 3, 2005) that seemed like it might contain a
solution, but that didn't work, either. Based on that thread, what I
have at the moment is something like:

thread = Thread.new {
# call to remote server
sleep 2
}
thread.join

I've tried putting the loop inside the thread and creating the thread
within the loop, but neither has worked. It always exits after the
sleep call. Does anyone have an idea about what might be wrong here,
or is there another way I can build in the delay?

Schuyler
 
C

ChrisH

Including the code (or a runnable example that exhibits the issue) is
always a good way to encourage a helpful response...

Cheers
Chris
 
S

SkyFox

My apologies. A similar example that I've been using to try to
troubleshoot the problem is:

set = ["1", "2", "3"]
thread = Thread.new {
set.collect { |x|
puts "starting loop"
puts x
sleep 2
puts "end of loop"
}
}
thread.join

I've also tried putting the Thread.new/thread.join inside of
set.collect, but the same problem happens. It will output:

starting loop
1
 
R

Robert Klemme

SkyFox said:
My apologies. A similar example that I've been using to try to
troubleshoot the problem is:

set = ["1", "2", "3"]
thread = Thread.new {
set.collect { |x|
puts "starting loop"
puts x
sleep 2
puts "end of loop"
}
}
thread.join

I've also tried putting the Thread.new/thread.join inside of
set.collect, but the same problem happens. It will output:

starting loop
1

I don't know what you're doing but this code definitively works ok - from
theory as well as from testing:

Robert@Babelfish2 ~
$ ruby <<EEE
set = ["1", "2", "3"]
thread = Thread.new {
set.collect { |x|
puts "starting loop"
puts x
sleep 2
puts "end of loop"
}
}
thread.join
EEE
starting loop
1
end of loop
starting loop
2
end of loop
starting loop
3
end of loop

Robert@Babelfish2 ~

Please recheck which code you tested and then repost the correct code.

robert
 
S

SkyFox

Hi Robert,

It was the correct code; I switched boxes and it works on the other
system. Apparently there's something strange going on with the system
I was working on. Of course, I would discover it was something that
simple after I gave up and posted to the group! At least I know I'm
not going crazy (yet).

Thanks all for your help.

Schuyler

Robert said:
SkyFox said:
My apologies. A similar example that I've been using to try to
troubleshoot the problem is:

set = ["1", "2", "3"]
thread = Thread.new {
set.collect { |x|
puts "starting loop"
puts x
sleep 2
puts "end of loop"
}
}
thread.join

I've also tried putting the Thread.new/thread.join inside of
set.collect, but the same problem happens. It will output:

starting loop
1

I don't know what you're doing but this code definitively works ok - from
theory as well as from testing:

Robert@Babelfish2 ~
$ ruby <<EEE
set = ["1", "2", "3"]
thread = Thread.new {
set.collect { |x|
puts "starting loop"
puts x
sleep 2
puts "end of loop"
}
}
thread.join
EEE
starting loop
1
end of loop
starting loop
2
end of loop
starting loop
3
end of loop

Robert@Babelfish2 ~

Please recheck which code you tested and then repost the correct code.

robert
 
R

Robert Klemme

SkyFox said:
Hi Robert,

It was the correct code; I switched boxes and it works on the other
system. Apparently there's something strange going on with the system
I was working on.

Weird. Output buffering?
Of course, I would discover it was something that
simple after I gave up and posted to the group! At least I know I'm
not going crazy (yet).

Don't worry - that can happen later still. :))
Thanks all for your help.

You're welcome!

robert
 
C

ChrisH

Robert said:
SkyFox wrote: ....
...

I recall one of the Pragmatic Programmers had a threading issue (on Mac
OS X ISTR) and it turned out to the installed ruby wasn't built with
thread support? Or was missing a library?

As you have determined, a re-instal or re-build should get you going

Cheers
 

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
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top