ruby Ruby on Rails中的分叉进程

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby Ruby on Rails中的分叉进程相关的知识,希望对你有一定的参考价值。

  # Logic for forking connections
  # The forked process does not have access to static vars as far as I can discern, so I've done some stuff to check if the op threw an exception.
  def fork_with_new_connection
    # Store the ActiveRecord connection information
    config = ActiveRecord::Base.remove_connection

    pid = fork do
      # tracking if the op failed for the Process exit
      success = true

      begin
        ActiveRecord::Base.establish_connection(config)
        
        # This is needed to re-initialize the random number generator after forking (if you want diff random numbers generated in the forks)
        srand

        # Run the closure passed to the fork_with_new_connection method
        yield

      rescue Exception => exception
        puts ("Forked operation failed with exception: " + exception)
        
        # the op failed, so note it for the Process exit
        success = false

      ensure
        ActiveRecord::Base.remove_connection
        Process.exit! success
      end
    end

    # Restore the ActiveRecord connection information
    ActiveRecord::Base.establish_connection(config)

    #return the process id
    pid
  end 


  # A static var to keep track of the number of failures
  @@failed = 0
  @@forks = 20

  #forks @@forks processes
  def concurrent_opps iteration
    (1..@@forks).each do |i|
      pid = fork_with_new_connection do
        # simple way to keep track of progress
        puts "Fork: #{i}"

        #necessary to manage activerecord connections since we are forking
        ActiveRecord::Base.connection.reconnect!

        #
        # The whole reason for creating the fork... do some stuff here
        #
        x = rand[25]
        if 0==x then raise Exception "it was 6, and I don't like 6's" end
      end
      
      puts "Process #{pid} completed"
    end
    
    #Wait for all processes to finish before proceeding - collect results as well 
    results = Process.waitall 

    results.each{ |result|
      @@failed += result[1].exitstatus
    }
  end

以上是关于ruby Ruby on Rails中的分叉进程的主要内容,如果未能解决你的问题,请参考以下文章

ruby/ruby on rails 内存泄漏检测

Ruby on Rails - Ruby 中的 KeyCode [关闭]

如何在 ruby​​ on rails 中访问 rails 助手和嵌入资产 javascript 文件中的 ruby​​?

Ruby on Rails 中的 Runner

Ruby on Rails:无法加载rack / handler /

Cpanel 中的 Ruby On Rails 问题