How to run Rails multi-threaded in development?

Viewed 6970

I am working on a multiple projects that talk to each other sometimes and I've run into an issue where app

  1. A calls B (request 1, still running)
  2. B calls A (request 2)
  3. based on request 2's result, B responds to request 1

This requires me running multi-threaded rails in development mode.

I know I can set it up using puma or something like that but ... Isn't there really a simpler way?

I would like to avoid changing anything in the project (adding gems, config files..).

Something like rails s --multi would be nice, can't webrick just run with multiple threads or spawn more processes?

Can I perhaps install a standalone gem to do what I need and run something like thin run . -p 3?

3 Answers

The puma web server can provide multi-threading and multiple workers bound to a single local address.

  1. Install the puma gem:

    bundle add puma
    

    or

    gem install puma
    
  2. Add a puma configuration file at config/puma.rb:

    workers 1 # 1 worker in addition to master instance (i.e. handle 2 requests concurrently).
    preload_app!
    
  3. Launch the Rails server.

    bundle exec rails s
    

    Puma automatically starts and loads in the config file at config/puma.rb.

Bump up the value for workers if you need to handle more than 2 concurrent requests at the same time.

My current solution, that's super kludgy, is to use Foreman and a Procfile to run two copies of my app on different ports. You'd have to configure your B service to make requests to the secondary port.

Related