Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6798085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:41:15+00:00 2026-05-26T18:41:15+00:00

we use Rails and EventMachine together, and when using that combo with Passenger there

  • 0

we use Rails and EventMachine together, and when using that combo with Passenger there is some very specific setup that needs to be done. After a lot of trial and error, I got EventMachine initialization working well, but I would like to understand the code a little better. As you can see below in this code snippet, our initializer checks for Passenger, and then checks if it’s a forked process before restarting EventMachine.

if defined?(PhusionPassenger)
  PhusionPassenger.on_event(:starting_worker_process) do |forked|
  # for passenger, we need to avoid orphaned threads
    if forked && EM.reactor_running?
      EM.stop
    end
    Thread.new {
      EM.run do

My question is related to the EM.reactor_running? and EM.stop commands. If Passenger has forked our process, why do I need to restart the EM reference in a new thread? If EM.reactor_running? returns true, what EM instance am I referencing?

You can see the full initializer code on our blog here http://www.hiringthing.com/2011/11/04/eventmachine-with-rails.html

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:41:15+00:00Added an answer on May 26, 2026 at 6:41 pm

    First of all, there’s only one EventMachine instance per Ruby process, so no matter what you’ll always reference the same EM instance, independent of the thread your currently in.

    You run the reactor in a new, separate thread so that it doesn’t block the main thread (whose purpose it is to serve the web request). EM.run would otherwise take over control, entering its run loop, not leaving the EM.run block anymore. EM.reactor_running? returns true, well, if an EM loop is running somewhere. As there is only one per Ruby process it’s easy enough for the method to figure out if EM is running or not.

    The setup you have here is the simplest way to use EM inside a normal Ruby process without interfering with everything else that’s running. I’m assuming you’re pushing messages to an AMQP broker from your web app. Whenever you send a message, it will go into EM’s run loop in the separate thread, that part is pretty transparent to you, and not affect the main loop, which can continue handling the Rails web request. Be careful though to always push things onto the EM loop using EM.next_tick. Trying to handle sockets opened by EM in different threads could result in bad things happening, which I have seen in production, incidentally by using and building a library called happening 😉

    Stopping the EM loop before starting a new one takes care of an EM loop that may be left over from a parent process, which could result in problems with file descriptors opened, using EM, in the parent process. In custom code this can be circumvented by using EM.fork_reactor, but as the parent process is out of your control, it’s safest to check if a reactor exists and stop it before starting a new instance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So when you use scaffolding Rails will kick out some layouts that are xhtml
Is it architecturally sound to use Rails and Oracle? My concern is that Oracle
I use the rails 3.0.0.beta Is there any new syntax to write RJS, here
I am trying to use some rails code withing a javascript and need to
I'm trying to use Rails 3.2.0.rc2 but I'm running into some problems. When I
I want to use rails console and I'm using ubuntun 11.10 currently I have
When I use rails c , I could type repo = Grit::Repo.name(/path/path) and there
I am very new to Ruby and Rails. I use rails 3.0.9 and ruby
I am using Rails v2.3 for my project. Then I decided to use Rails
I'm building a Rails application that requires some models to interact with some necessary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.