# Process-per-connection with drb

**URL:** <https://www.ruby-forum.com/t/process-per-connection-with-drb/83941>\
**Category:** Ruby\
**Created:** [January 30, 2007, 11:07pm UTC](https://www.ruby-forum.com/t/process-per-connection-with-drb/83941 "2007-01-30T23:07:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [January 30, 2007, 11:07pm UTC](https://www.ruby-forum.com/t/process-per-connection-with-drb/83941/1 "2007-01-30T23:07:02Z")

</div>

Reading through drb.rb, I see that drb uses a thread-per-connection  
model. I have some C++ extensions that I want to provide access to via  
a drb server. Unfortunately, these extensions are not safe to use with  
ruby’s green threads, and it is impossible to make them thread-safe,  
given the mechanism ruby uses to switch thread contexts.

I want to instead use a process-per-connection model and have a pool of  
processes waiting to handle requests. How should I go about  
implementing this?

Paul

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [January 31, 2007, 5:17am UTC](https://www.ruby-forum.com/t/process-per-connection-with-drb/83941/2 "2007-01-31T05:17:28Z")

</div>

On 1/30/07, Paul B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Reading through drb.rb, I see that drb uses a thread-per-connection  
> model. I have some C++ extensions that I want to provide access to via  
> a drb server. Unfortunately, these extensions are not safe to use with  
> ruby’s green threads, and it is impossible to make them thread-safe,  
> given the mechanism ruby uses to switch thread contexts.
> 
> I want to instead use a process-per-connection model and have a pool of  
> processes waiting to handle requests. How should I go about  
> implementing this?

Well, I will make no comment about how you “should” go about this, but I  
hacked this together:

See: [http://raa.ruby-lang.org/project/slave/](http://raa.ruby-lang.org/project/slave/)

# slaved.rb:

require ‘drb’  
require ‘slave’

class SlaveDaemon  
attr\_reader :slave

def initialize klass  
puts “Initializing a new slave of class #{klass}.”  
@slave = Slave::new{ eval “#{klass}.new” }  
end  
end

DRb.start\_service(‘druby://localhost:9000’, SlaveDaemon)  
DRb.thread.join

#client.rb:  
require ‘drb’

DRb.start\_service()  
SlaveDaemon = DRbObject.new(nil, ‘druby://localhost:9000’)

s = SlaveDaemon.new(Array)  
a = s.slave.object

a[1] = 1  
a[2] = 2  
a[4] = 4

a.each do |i|  
p i  
end

> ruby slaved.rb  
> Initializing a new slave of class Array.

> ruby client.rb  
> nil  
> 1  
> 2  
> nil  
> 4

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [February 1, 2007, 2:08am UTC](https://www.ruby-forum.com/t/process-per-connection-with-drb/83941/3 "2007-02-01T02:08:27Z")

</div>

removed\_email\_address@domain.invalid wrote:

> On 1/30/07, Paul B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I want to instead use a process-per-connection model and have a pool of  
> > processes waiting to handle requests. How should I go about  
> > implementing this?  
> > Well, I will make no comment about how you “should” go about this, but I  
> > hacked this together:

There’s also ara’s ruby queues thing.

Devin
