如何在开发中运行Rails多线程?

时间:2022-10-23 21:01:59

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

我在做多个项目,有时会互相交流,我遇到了一个问题,app

  1. A calls B (request 1, still running)
  2. A调用B(请求1,仍在运行)
  3. B calls A (request 2)
  4. B调用A(请求2)
  5. based on request 2's result, B responds to request 1
  6. 基于请求2的结果,B响应请求1

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

这要求我在开发模式下运行多线程rails。

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

我知道我可以用puma或者类似的东西来设置它但是…难道没有更简单的方法吗?

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

我希望避免在项目中更改任何东西(添加gems、配置文件..)。

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

像rails s -multi -会很好,webrick就不能使用多个线程运行或者产生更多的进程吗?

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

我是否可以安装一个独立的gem来执行我需要的操作并运行一些类似于瘦运行的操作。- p 3 ?

3 个解决方案

#1


2  

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

解决这个问题的一种方法是使用POW,它默认使用两个worker。

The nice thing is I don't have to modify the project files to do it so it satisfies my requirements.

好处是,我不必修改项目文件来完成它,这样它就能满足我的需求。

#2


0  

You can configure your app to be multi-threaded by un-commenting the following line from production.rb:

您可以将您的应用程序配置为多线程的,方法是取消对production.rb中的以下内容的注释:

# config.threadsafe!

# config.threadsafe !

If you run RAILS_ENV=production bundle exec rails server you'll start up in production mode with multi-threading. You'll probably have to cross the Puma bridge either way, though, if and when you deploy to a production server.

如果您运行RAILS_ENV=production bundle exec rails服务器,您将使用多线程启动生产模式。不过,在部署到生产服务器时,您可能必须以任何一种方式跨越Puma桥接。

#3


0  

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.

我目前的解决方案是使用Foreman和Procfile在不同的端口上运行我的两个应用程序。您必须配置您的B服务以向辅助端口发出请求。

#1


2  

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

解决这个问题的一种方法是使用POW,它默认使用两个worker。

The nice thing is I don't have to modify the project files to do it so it satisfies my requirements.

好处是,我不必修改项目文件来完成它,这样它就能满足我的需求。

#2


0  

You can configure your app to be multi-threaded by un-commenting the following line from production.rb:

您可以将您的应用程序配置为多线程的,方法是取消对production.rb中的以下内容的注释:

# config.threadsafe!

# config.threadsafe !

If you run RAILS_ENV=production bundle exec rails server you'll start up in production mode with multi-threading. You'll probably have to cross the Puma bridge either way, though, if and when you deploy to a production server.

如果您运行RAILS_ENV=production bundle exec rails服务器,您将使用多线程启动生产模式。不过,在部署到生产服务器时,您可能必须以任何一种方式跨越Puma桥接。

#3


0  

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.

我目前的解决方案是使用Foreman和Procfile在不同的端口上运行我的两个应用程序。您必须配置您的B服务以向辅助端口发出请求。