[Backgroundrb-devel] Cutting down BackgrounDRb memory issue

hemant kumar gethemant at gmail.com
Sun Apr 13 21:21:09 EDT 2008


On Fri, 2008-04-11 at 20:54 -0400, Ryan Leavengood wrote:
> On Fri, Apr 11, 2008 at 8:22 PM, hemant kumar <gethemant at gmail.com> wrote:
> >
> >  A classic solution is to use fork and exec, rather than just fork. Its
> >  working and pretty stable, but you loose ability to pass complex
> >  arguments when invoking MiddleMan.new_worker, because essentially I can
> >  only pass arguments in form of strings when using exec.
> 
> Is that really a problem?
> 
> irb(main):001:0> a = [{:key=>'value','another one'=>'something'}, 3,
> 56, "foobar"]
> => [{"another one"=>"something", :key=>"value"}, 3, 56, "foobar"]
> irb(main):002:0> s = Marshal.dump(a)
> => "\004\b[\t{\a\"\020another one\"\016something:\bkey\"\nvaluei\bi=\"\vfoobar"
> irb(main):003:0> b = Marshal.load(s)
> => [{"another one"=>"something", :key=>"value"}, 3, 56, "foobar"]
> irb(main):004:0> a == b
> => true

Nevermind, that wasn't a problem at all. As a simple fix, I am avoiding
passing anything other than file descriptors as command line argument
and before forking and execing the process, I am writing worker_options
to write end of master process. Seems to be working well.

Something like this:

require "socket"

def enable_nonblock io
  f = io.fcntl(Fcntl::F_GETFL,0)
  io.fcntl(Fcntl::F_SETFL,Fcntl::O_NONBLOCK | f)
end


master_read_end,worker_write_end = UNIXSocket.pair(Socket::SOCK_STREAM)

worker_read_end,master_write_end = UNIXSocket.pair(Socket::SOCK_STREAM)

master_write_end.puts "Hello from parent"

if(!(pid = fork))
  [master_write_end,master_read_end].each { |x| x.close }
  [worker_read_end,worker_write_end].each {  |x| enable_nonblock(x) }
  exec("bar.rb","#{worker_read_end.fileno}:#{worker_write_end.fileno}")
end

[master_write_end,master_read_end].each { |x| enable_nonblock(x) }
p pid

worker_read_end.close
worker_write_end.close

a = master_read_end.gets
puts a





More information about the Backgroundrb-devel mailing list