# \[ANN\] forkoff - parallel processing for ruby enumerables

**URL:** https://rubytalk.org/t/ann-forkoff-parallel-processing-for-ruby-enumerables/45958
**Category:** ruby-talk
**Created:** [18 April 2008 01:43 UTC](https://rubytalk.org/t/ann-forkoff-parallel-processing-for-ruby-enumerables/45958 "2008-04-18T01:43:05Z")
**Posts on this page:** 3
**Page:** 2

<div class="post-metadata">

### Author: ![a11](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/a11/32/8169_2.png) [@a11](https://rubytalk.org/u/a11)
#### Post date: [18 April 2008 15:59 UTC](https://rubytalk.org/t/ann-forkoff-parallel-processing-for-ruby-enumerables/45958/21 "2008-04-18T15:59:44Z")

</div>

yes. forkoff has a number of consumer \*green\* threads used to manage an array of queues containing the elements destined to be passed to a forked process/native thread for execution of the block. the code is very short, give a read.

cheers.

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

> **···**
>
> On Apr 18, 2008, at 6:23 AM, fedzor wrote:
> 
> > Since it's using Kernel#fork(), does this mean it is using OS threads?
> 
> --  
> we can deny everything, except that we have the possibility of being better. simply reflect on that.  
> h.h. the 14th dalai lama

---

<div class="post-metadata">

### Author: ![a11](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/a11/32/8169_2.png) [@a11](https://rubytalk.org/u/a11)
#### Post date: [29 April 2008 23:02 UTC](https://rubytalk.org/t/ann-forkoff-parallel-processing-for-ruby-enumerables/45958/22 "2008-04-29T23:02:36Z")

</div>

require 'timeout'

begin  
&nbsp;&nbsp;&nbsp;Timeout.timeout(seconds){ thread.join }  
rescue Timeout::Error  
&nbsp;&nbsp;&nbsp;thread.kill rescue nil  
end

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

> **···**
>
> On Apr 29, 2008, at 4:15 PM, Abdul-rahman Advany wrote:
> 
> > Sorry, just figured out calling fork makes the thread a child process...  
> > just need to find out how to set a timeout on the thread... anyone  
> > suggestions?
> 
> --  
> we can deny everything, except that we have the possibility of being better. simply reflect on that.  
> h.h. the 14th dalai lama

---

<div class="post-metadata">

### Author: ![Jeremy\_Hinegardner](https://avatars.discourse-cdn.com/v4/letter/j/65b543/32.png) [@Jeremy\_Hinegardner](https://rubytalk.org/u/Jeremy_Hinegardner)
#### Post date: [30 April 2008 15:32 UTC](https://rubytalk.org/t/ann-forkoff-parallel-processing-for-ruby-enumerables/45958/23 "2008-04-30T15:32:45Z")

</div>

That'll kill the thread, but not the child process that was forked, at least  
that's what I remember.

Try this as a general strategy:

&nbsp;&nbsp;parent\_t = Thread.new(cmd) do |exec\_me|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if cpid = fork then  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thread.current[:cpid] = cpid  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cpid, exit\_status = Process.waitpid2(cpid)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thread.current[:exit\_status] = exit\_status  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exec exec\_me  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;unless parent\_t.join( seconds ) # seconds contains your timeout value  
&nbsp;&nbsp;cpid = parent\_t[:cpid]  
&nbsp;&nbsp;%w[TERM KILL].each do |sig|  
&nbsp;&nbsp;&nbsp;&nbsp;Process.kill(sig, cpid)  
&nbsp;&nbsp;&nbsp;&nbsp;break if parent\_t.join(1)  
&nbsp;&nbsp;end

&nbsp;&nbsp;# Do something with Thread.current[:exit\_status] to report the child  
&nbsp;&nbsp;# process exit status.

enjoy,

-jeremy

> **···**
>
> On Wed, Apr 30, 2008 at 08:02:36AM +0900, ara.t.howard wrote:
> 
> > On Apr 29, 2008, at 4:15 PM, Abdul-rahman Advany wrote:
> > 
> > > Sorry, just figured out calling fork makes the thread a child process...  
> > > just need to find out how to set a timeout on the thread... anyone  
> > > suggestions?
> > 
> > require 'timeout'
> > 
> > begin  
> > &nbsp;&nbsp;Timeout.timeout(seconds){ thread.join }  
> > rescue Timeout::Error  
> > &nbsp;&nbsp;thread.kill rescue nil  
> > end
> 
> # --
> 
> Jeremy Hinegardner jeremy@hinegardner.org

[Previous page](https://rubytalk.org/t/ann-forkoff-parallel-processing-for-ruby-enumerables/45958.md?page=1)
