How do I run Rails multithreading in development?

I am working on several projects that sometimes talk to each other and I am facing an issue where the application

  • Call B ( request 1

    , still works)
  • B calls A ( request 2

    )
  • based on the result request 2

    , B responds to query 1

This requires me to run multi-threaded rails in development mode.

I know I can set it up with puma or something, but ... isn't there an easier way?

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

Something like rails s --multi

that would be nice, can't webrick

run with multiple threads or spawn more processes?

Can I possibly install a separate gem to do what I need and run something like thin run . -p 3

?

+5


source to share


4 answers


One way to solve this problem is to use POW , which uses two workers by default.



The nice thing is that I don't need to modify the project files to fit my requirements.

+2


source


The puma web server can provide multithreading and multiple workers tied to the same local address.

  1. Install the puma gem:

    bundle add puma
    
          

    or

    gem install puma
    
          

  2. Add the Puma config file to config/puma.rb

    :

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

  3. Start the Rails server.

    bundle exec rails s
    
          

    Puma automatically starts and loads into a config file in config/puma.rb

    .



Increase the value for workers

if you need to handle more than 2 simultaneous requests.

+2


source


My current solution, which is super kludgy, is to use Foreman and Procfile to run two copies of my application on different ports, you will need to configure service B to request an additional port.

+1


source


You can configure your application to multithread without commenting out the following line from production.rb:

# config.threadsafe!

If you run RAILS_ENV=production bundle exec rails server

, you will run in multi-threaded production mode. You will probably have to cross the Puma bridge anyway, though, if and when you will be deploying to a production server.

0


source







All Articles