Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Threads - running something in background

Reply
Thread Tools

Threads - running something in background

 
 
Alex Zunega
Guest
Posts: n/a
 
      09-03-2009
Hey guys,

I've been studying Ruby for some weeks now but when it comes to
threads, I know that I am totally unskilled.

I am trying to build a class which is able to keep updating itself every
X seconds after being initialized. In general terms it would work like
this:

----
class Threading_Test
attr_accessor :i
def initialize
@i = 0
@j = 0
neverstop = false

t1 = Thread.new do
while neverstop == false do
sleep 0.1 #here's the the problem.
@i = @i + 1
end
end

t1.run

puts "I am ready and I keep updating myself. Or not..."
end
end
----


The problem occurs when I try to add this "sleep 0.1" into the thread
block. The thread only seems to become active when I ask for the @i
value.

I know that there might be other ways to implement that, but i think
that it would fit my code better that setting up a Cron task to do so.

Thank you all for the help.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
7stud --
Guest
Posts: n/a
 
      09-03-2009
Alex Zunega wrote:
> Hey guys,
>
> I've been studying Ruby for some weeks now but when it comes to
> threads, I know that I am totally unskilled.
>
> I am trying to build a class which is able to keep updating itself every
> X seconds after being initialized. In general terms it would work like
> this:
>
> ----
> class Threading_Test
> attr_accessor :i
> def initialize
> @i = 0
> @j = 0
> neverstop = false
>
> t1 = Thread.new do
> while neverstop == false do
> sleep 0.1 #here's the the problem.
> @i = @i + 1
> end
> end
>
> t1.run
>
> puts "I am ready and I keep updating myself. Or not..."
> end
> end
> ----
>
>
> The problem occurs when I try to add this "sleep 0.1" into the thread
> block. The thread only seems to become active when I ask for the @i
> value.
>


I'm not sure what you mean. Try this:

class Updater
attr_accessor :i

def initialize
@i = 0

Thread.new do
loop do
sleep(1)
@i += 1
puts "....#{@i}"
end
end

end

end


upd = Updater.new

5.times do
sleep(2)
puts upd.i
end

Here is my output:
....1
1
....2
....3
3
....4
....5
5
....6
....7
7
....8
....9
9

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

 
Reply With Quote
 
 
 
 
7stud --
Guest
Posts: n/a
 
      09-03-2009
Alex Zunega wrote:
> Hey guys,
>
> I've been studying Ruby for some weeks now but when it comes to
> threads, I know that I am totally unskilled.
>


Also, you should realize that the @i in the class and the @i in the
thread are the same variable.
Therefore, things like this can happen:

class Updater
attr_accessor :i

def initialize
@i = 0

Thread.new do
loop do
sleep(0.3)
@i += 1
puts "....#{@i}"
end
end

end

end


upd = Updater.new

3.times do
sleep(2)
puts upd.i

upd.i = 0 #<-----******
end

--output:--
....1
....2
....3
....4
....5
....6
6
....1
....2
....3
....4
....5
....6
....7
7
....1
....2
....3
....4
....5
....6
6


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

 
Reply With Quote
 
Alex Zunega
Guest
Posts: n/a
 
      09-03-2009
7stud -- wrote:
> Alex Zunega wrote:
>> Hey guys,
>>
>> I've been studying Ruby for some weeks now but when it comes to
>> threads, I know that I am totally unskilled.
>>

>
> Also, you should realize that the @i in the class and the @i in the
> thread are the same variable.
> Therefore, things like this can happen:
>
> class Updater
> attr_accessor :i
>
> def initialize
> @i = 0
>
> Thread.new do
> loop do
> sleep(0.3)
> @i += 1
> puts "....#{@i}"
> end
> end
>
> end
>
> end
>
>
> upd = Updater.new
>
> 3.times do
> sleep(2)
> puts upd.i
>
> upd.i = 0 #<-----******
> end
>
> --output:--
> ....1
> ....2
> ....3
> ....4
> ....5
> ....6
> 6
> ....1
> ....2
> ....3
> ....4
> ....5
> ....6
> ....7
> 7
> ....1
> ....2
> ....3
> ....4
> ....5
> ....6
> 6



Hey 7stud,

Thanks for the very prompt answer. I couldn't make myself clear and I'll
try harder this time...

I've just removed two lines from the code you genially sent me. Removing
these two lines I got the behavior I would like to have. See the code
and results below:

>> class Updater
>> attr_accessor :i
>>

?> def initialize
>> @i = 0
>>

?> Thread.new do
?> loop do
?>
?> @i += 1
>>

?> end
>> end
>>

?> end
>>

?> end
=> nil
>> x.i

=> 213336
>> x.i

=> 237848
>>


I just want something to time these iteration (one per second, for
example), and everytime that I insert some "sleep" method, the Updater
would "sum 1" to @i only when I try to access @i attribute.

What I expect, in other words, is to have an @i very close to the number
of seconds of the object life, just to prove that it works...


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

 
Reply With Quote
 
7stud --
Guest
Posts: n/a
 
      09-03-2009
Alex Zunega wrote:
>
> I just want something to time these iteration (one per second, for
> example), and everytime that I insert some "sleep" method, the Updater
> would "sum 1" to @i only when I try to access @i attribute.
>
> What I expect, in other words, is to have an @i very close to the number
> of seconds of the object life, just to prove that it works...


You aren't making any sense. On the one hand, you say that you want @i
to tell you the number of seconds the object has been "alive". On the
other hand, you say you only want to add 1 to @i when you try to access
@i. Which is it?


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

 
Reply With Quote
 
Alex Zunega
Guest
Posts: n/a
 
      09-03-2009
7stud -- wrote:
> Alex Zunega wrote:
>>
>> I just want something to time these iteration (one per second, for
>> example), and everytime that I insert some "sleep" method, the Updater
>> would "sum 1" to @i only when I try to access @i attribute.
>>
>> What I expect, in other words, is to have an @i very close to the number
>> of seconds of the object life, just to prove that it works...

>
> You aren't making any sense. On the one hand, you say that you want @i
> to tell you the number of seconds the object has been "alive". On the
> other hand, you say you only want to add 1 to @i when you try to access
> @i. Which is it?


Actually, you're right, I was not clear, but I meant the second one.
After initialized, 10 seconds later, when retrieving @i value I would
like to see @i as 10, meaning that for 10 times it updated itself.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
7stud --
Guest
Posts: n/a
 
      09-03-2009
I give up.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Douglas Seifert
Guest
Posts: n/a
 
      09-04-2009
7stud's original Updater class seems to meet the requirements.=A0 Are
you trying to write a class that will perform some work and then sleep
1.0 seconds and keep a count of the number of iterations it performed?
Or are you trying to right a class that will perform some work
exactly once per second?=A0 In the first case, your work will not
generally fire once a second because the work itself will take a non
zero amount of time.=A0 The second case is trickier ... maybe this would
work:

class PeriodicWorker
=A0 def initialize(period =3D 1.0)
=A0=A0=A0 @period =3D period
=A0 end

=A0 def run
=A0=A0=A0 start =3D Time.now.to_f
=A0=A0=A0 while true
=A0=A0=A0=A0=A0 yield
=A0=A0=A0=A0=A0 sleep_time =3D (start + @period) - Time.now.to_f
=A0=A0=A0=A0=A0 sleep(sleep_time) rescue nil # Punt if the worker took long=
er
than @period seconds to run
=A0=A0=A0=A0=A0 start +=3D @period
=A0=A0=A0 end
=A0 end
end

worker =3D PeriodicWorker.new

worker.run do
=A0puts "Time: #{Time.now.to_f}"
=A0sleep 0.22
=A0puts "Finished my work: #{Time.now.to_f}"
end


The only issue with this class is if the work takes longer than the
period, you are creating a spinloop that will suck up 100% of your
CPU.

-Doug Seifert

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Threads - Get running threads Pedro Pinto Java 2 04-08-2008 11:44 PM
Background transparent when 'background' is used JWL HTML 4 09-26-2006 05:37 PM
Background Check - Background search - People search mason66 ASP .Net 0 07-27-2006 10:20 AM
Re: long-running background threads bruce barker ASP .Net 0 06-07-2004 06:26 PM
Re: long-running background threads David Browne ASP .Net 0 06-07-2004 05:52 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57