# ForkAndReturn 0.1.0

**URL:** <https://www.ruby-forum.com/t/forkandreturn-0-1-0/142914>\
**Category:** Ruby\
**Created:** [July 13, 2008, 12:23pm UTC](https://www.ruby-forum.com/t/forkandreturn-0-1-0/142914 "2008-07-13T12:23:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![erikveen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/59ef9b/32.png) [@erikveen](https://www.ruby-forum.com/u/erikveen)\
**Post date:** [July 13, 2008, 12:23pm UTC](https://www.ruby-forum.com/t/forkandreturn-0-1-0/142914/1 "2008-07-13T12:23:15Z")

</div>

ForkAndReturn 0.1.0 is released.

RDoc: [http://www.erikveen.dds.nl/forkandreturn/doc/index.html](http://www.erikveen.dds.nl/forkandreturn/doc/index.html)  
Download: [http://rubyforge.org/projects/forkandreturn/index.html](http://rubyforge.org/projects/forkandreturn/index.html)

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

* * *

ForkAndReturn implements a couple of methods that simplifies  
running a block of code in a subprocess. The result (Ruby  
object or exception) of the block will be available in the  
parent process.

The intermediate return value (or exception) will be  
Marshal’led to disk. This means that it is possible to  
(concurrently) run thousands of child process, with a relative  
low memory footprint. Just gather the results once all child  
process are done. ForkAndReturn will handle the writing,  
reading and deleting of the temporary file.

The core of these methods is fork\_and\_return\_core(). It returns  
some nested lambdas, which are handled by the other methods and  
by Enumerable#concurrent\_collect(). These lambdas handle the  
WAITing, LOADing and RESULTing (explained in  
fork\_and\_return\_core()).

The child process exits with Process.exit!(), so at\_exit()  
blocks are skipped in the child process. However, both $stdout  
and $stderr will be flushed.

Only Marshal’lable Ruby objects can be returned.

ForkAndReturn uses Process.fork(), so it only runs on platforms  
where Process.fork() is implemented.

Example:

[1, 2, 3, 4].collect do |object|  
Thread.fork do  
ForkAndReturn.fork\_and\_return do  
2\*object  
end  
end  
end.collect do |thread|  
thread.value  
end # ===\> [2, 4, 6, 8]

This runs each “2\*object” in a seperate process. Hopefully, the  
processes are spread over all available CPU’s. That’s a simple  
way of parallel processing! Although  
Enumerable#concurrent\_collect() is even simpler:

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

Note that the code in the block is run in a seperate process,  
so updating objects and variables in the block won’t affect the  
parent process:

count = 0  
[…].concurrent\_collect do  
count += 1  
end  
count # ==\> 0

Enuemerable#concurrent\_collect() is suitable for handling a  
couple of very CPU intensive jobs, like parsing large XML files.

Enuemerable#clustered\_concurrent\_collect() is suitable for  
handling a lot of not too CPU intensive jobs. The situations  
where the overhead of forking is too expensive, but where you  
still want to use all available CPU’s.

---

<div class="post-metadata">

**Author:** ![erikveen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/59ef9b/32.png) [@erikveen](https://www.ruby-forum.com/u/erikveen)\
**Post date:** [July 13, 2008, 12:48pm UTC](https://www.ruby-forum.com/t/forkandreturn-0-1-0/142914/2 "2008-07-13T12:48:30Z")

</div>

Erik V. wrote:

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

This looks great! Is there something special I should consider under  
Windows?

Kind regards  
Andreas

---

<div class="post-metadata">

**Author:** ![erikveen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/59ef9b/32.png) [@erikveen](https://www.ruby-forum.com/u/erikveen)\
**Post date:** [July 13, 2008, 12:59pm UTC](https://www.ruby-forum.com/t/forkandreturn-0-1-0/142914/3 "2008-07-13T12:59:30Z")

</div>

> This looks great! Is there something special I should consider under Windows?

ForkAndReturn uses Process.fork(), so it only runs on platforms where  
Process.fork() is implemented. Ruby for Windows hasn’t implemented  
Process.fork(), so ForkAndReturn won’t work on pure Windows. However,  
Ruby for Cygwin has implemented Process.fork().

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

---

<div class="post-metadata">

**Author:** ![erikveen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/59ef9b/32.png) [@erikveen](https://www.ruby-forum.com/u/erikveen)\
**Post date:** [July 14, 2008, 12:22am UTC](https://www.ruby-forum.com/t/forkandreturn-0-1-0/142914/4 "2008-07-14T00:22:35Z")

</div>

Erik V. wrote:

> > This looks great! Is there something special I should consider under Windows?
> 
> ForkAndReturn uses Process.fork(), so it only runs on platforms where  
> Process.fork() is implemented. Ruby for Windows hasn’t implemented  
> Process.fork(), so ForkAndReturn won’t work on pure Windows. However,  
> Ruby for Cygwin has implemented Process.fork().

Damn, this means it won’t run on jRuby either ☹  
I keep missing for on all kinds of cases…  
V.-
