[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Exits after sleep; ideas?

SkyFox

6/19/2006 7:40:00 PM

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

7 Answers

Chris Hulan

6/19/2006 7:45:00 PM

0

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

Cheers
Chris

SkyFox

6/19/2006 7:58:00 PM

0

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
> exit

ChrisH wrote:
> Including the code (or a runnable example that exhibits the issue) is
> always a good way to encourage a helpful response...
>
> Cheers
> Chris

Tim Hoolihan

6/19/2006 8:17:00 PM

0

Threading will defeat the purpose of the sleep. While one thread sleeps
another will take the opportunity to execute. Thereby hitting the
server more often than the sleep amount.

SkyFox wrote:
> 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
>> exit
>
> ChrisH wrote:
>> Including the code (or a runnable example that exhibits the issue) is
>> always a good way to encourage a helpful response...
>>
>> Cheers
>> Chris
>

Robert Klemme

6/19/2006 8:41:00 PM

0

SkyFox <skyfoxny@yahoo.com> wrote:
> 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
>> exit

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

SkyFox

6/19/2006 8:47:00 PM

0

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 Klemme wrote:
> SkyFox <skyfoxny@yahoo.com> wrote:
> > 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
> >> exit
>
> 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

Robert Klemme

6/20/2006 12:36:00 PM

0

SkyFox wrote:
> 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

Chris Hulan

6/20/2006 1:42:00 PM

0


Robert Klemme wrote:
> SkyFox wrote:
....
> > 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.
> ...

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