Threads - running something in background

A

Alex Zunega

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

7stud --

Alex said:
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
 
7

7stud --

Alex said:
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
 
A

Alex Zunega

7stud said:
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:
?> Thread.new do
?> loop do
?>
?> @i += 1?> end
=> nil
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...
 
7

7stud --

Alex said:
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?
 
A

Alex Zunega

7stud said:
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.
 
D

Douglas Seifert

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
 

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

Similar Threads

background threads 5
Threads 1
Don't wait for Exception in threads 3
question on threads 3
Understanding Threads... 3
Threads preventing garbage collection? 11
Ruby & Threads 3
Threads + Forks in Ruby 1.9.1p129 5

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top