# Running methods in parallel

**URL:** <https://www.ruby-forum.com/t/running-methods-in-parallel/144368>\
**Category:** Ruby\
**Created:** [July 29, 2008, 3:50am UTC](https://www.ruby-forum.com/t/running-methods-in-parallel/144368 "2008-07-29T03:50:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![skaverat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/779978/32.png) [@skaverat](https://www.ruby-forum.com/u/skaverat)\
**Post date:** [July 29, 2008, 3:50am UTC](https://www.ruby-forum.com/t/running-methods-in-parallel/144368/1 "2008-07-29T03:50:31Z")

</div>

is there a way to parallelize ruby methods?

## So I can do something like this:

5.times do  
Foobar.new(“somestuff”)  
end

#do stuff while Foobar-objects are running

* * *

so the script creates 5 objects, and just lets them do their job, while  
running the rest of the code.

meh, I really hope you understand what i mean 😉

---

<div class="post-metadata">

**Author:** ![skaverat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/779978/32.png) [@skaverat](https://www.ruby-forum.com/u/skaverat)\
**Post date:** [July 29, 2008, 3:59am UTC](https://www.ruby-forum.com/t/running-methods-in-parallel/144368/2 "2008-07-29T03:59:09Z")

</div>

nvm

just found the Thread.new method.

---

<div class="post-metadata">

**Author:** ![skaverat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/779978/32.png) [@skaverat](https://www.ruby-forum.com/u/skaverat)\
**Post date:** [July 29, 2008, 5:29am UTC](https://www.ruby-forum.com/t/running-methods-in-parallel/144368/3 "2008-07-29T05:29:29Z")

</div>

Hi

On Jul 28, 2008, at 8:50 PM, Skave R. wrote:

> is there a way to parallelize ruby methods?

You can always use threads:  
[http://corelib.rubyonrails.org/classes/Thread.html](http://corelib.rubyonrails.org/classes/Thread.html)

> 5.times do  
> Foobar.new(“somestuff”)  
> end
> 
> #do stuff while Foobar-objects are running

5.times do  
Thread.new {  
Foobar.new(“somestuff”)  
}  
end

# do stuff while Foobar objects are running

---

<div class="post-metadata">

**Author:** ![skaverat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/779978/32.png) [@skaverat](https://www.ruby-forum.com/u/skaverat)\
**Post date:** [July 29, 2008, 5:30am UTC](https://www.ruby-forum.com/t/running-methods-in-parallel/144368/4 "2008-07-29T05:30:04Z")

</div>

> You can always use threads:

Threads are often a patch over bad architecture. You can also try this:

foobars = (0…5).map{ Foobar.new(“somestuff”) }

loop do  
foobars.each{|foobar| foobar.run\_one\_slice }  
end

Whatever foobar does, it must use some loop statement. If you put the  
loop on  
the outside, you will have a better architecture. Each call to  
run\_one\_slice  
performs one foobar activity. Foobar must store its state as instance  
variables,  
between each call to .run\_one\_slice. That forces Foobar to be more  
object-oriented, and more event-driven.

If you start with a good architecture, and measure it, you will know if  
its  
performance is adequate, or if it needs more help. Only then you add  
threads.  
And threads work best with event-driven architectures, so adding the  
thread last  
is always better than adding it first. Premature optimization is the  
root of all  
evil.

---

<div class="post-metadata">

**Author:** ![skaverat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/779978/32.png) [@skaverat](https://www.ruby-forum.com/u/skaverat)\
**Post date:** [August 2, 2008, 4:25pm UTC](https://www.ruby-forum.com/t/running-methods-in-parallel/144368/5 "2008-08-02T16:25:56Z")

</div>

Take a look at Forkoffhttp://rubyforge.org/projects/codeforpeople

and Peach  
[http://peach.rubyforge.org/](http://peach.rubyforge.org/)

dean

---

<div class="post-metadata">

**Author:** ![skaverat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/779978/32.png) [@skaverat](https://www.ruby-forum.com/u/skaverat)\
**Post date:** [August 2, 2008, 4:37pm UTC](https://www.ruby-forum.com/t/running-methods-in-parallel/144368/6 "2008-08-02T16:37:03Z")

</div>

> is there a way to parallelize ruby methods?

Have a look at ForkAndReturn [1,2]. ForkAndReturn implements a  
couple of methods that simplify running a block of code in a  
subprocess. The result (Ruby object or exception) of the block  
will be available in the parent process.

Here’s an example:

[1, 2, 3, 4].concurrent\_collect do |object|  
2\*object  
end # ===\> [2, 4, 6, 8]

This runs each “2\*object” in a seperate process. Hopefully, the  
processes are spread over all available CPU’s.

gegroet,  
Erik V. - [http://www.erikveen.dds.nl/](http://www.erikveen.dds.nl/)

[1] [forkandreturn (0.2.3)](http://www.erikveen.dds.nl/forkandreturn/doc/index.html)  
[2] [http://rubyforge.org/projects/forkandreturn/](http://rubyforge.org/projects/forkandreturn/)
