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
?
source to share
The puma web server can provide multithreading and multiple workers tied to the same local address.
Install the puma gem:
bundle add puma
or
gem install puma
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!
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.
source to share
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.
source to share