相关疑难解决方法(0)

Fork上的Fork,Ruby,ActiveRecord和文件描述符

我知道当我们分叉一个进程时,子进程会继承父进程打开文件描述符和偏移的副本.根据手册页,这指的是父母使用的相同文件描述符.基于该理论在以下程序中


puts "Process #{Process.pid}"

file = File.open('sample', 'w')

forked_pid = fork do
    sleep(10)
    puts "Writing to file now..."
    file.puts("Hello World. #{Time.now}")       
end




file.puts("Welcome to winter of my discontent #{Time.now}")
file.close
file = nil

问题1: 休眠10秒的分叉进程不应该丢失其文件描述符,并且在父进程完成并关闭文件并退出时无法写入文件.
问题2:但无论出于何种原因,如果这都有效,那么ActiveRecord在这种情况下如何失去连接.它只有在我设置:reconnect => trueActiveRecord连接时才能正常连接,这意味着它失去连接.


require "rubygems"
require "redis"
require 'active_record'
require 'mysql2'

connection = ActiveRecord::Base.establish_connection({
    :adapter => 'mysql2',
    :username => 'root_user',
    :password => 'Pi',
    :host => 'localhost',
    :database => 'list_development', 
    :socket => '/var/lib/mysql/mysql.sock'

    })

class User < ActiveRecord::Base   
end

u = User.first

puts …
Run Code Online (Sandbox Code Playgroud)

ruby activerecord fork ruby-on-rails file-descriptor

6
推荐指数
1
解决办法
649
查看次数