在rails中简单的AJAX调用上提高并发性

时间:2021-09-22 23:30:11

I have created a simple ajax call with the following code:

我用以下代码创建了一个简单的ajax调用:

controller.rb

def locations
    sleep 1.2
    some_data = [{"name"=> "chris", "age"=> "14"}]
    render json: some_data
end

view.js

function getLocation() {
     $.get('/location').success(function(data){console.log(data);});
}

$(".button").click(function() {getLocation();});

Routes.rb

get '/location' => 'controller#locations'

Note that the sleep 1.2 in the controller it is to prevent doing background jobs or database calls.

请注意,控制器中的sleep 1.2是为了防止执行后台作业或数据库调用。

The screenshot below is from the devtools Network tab, it shows I have clicked the button 8 times and all the subsequent calls are stalled until the previous action is finished. I think it is due to Rails being single threaded? Will it be a different case if the server is made with NodeJS? And How can I achieve similar concurrency with Rails for similar AJAX calls?

下面的截图来自devtools网络选项卡,它显示我已经点击了8次按钮,所有后续调用都停止,直到上一个操作完成。我认为这是由于Rails是单线程的?如果服务器是用NodeJS制作的,会不会是另一种情况?对于类似的AJAX调用,如何使用Rails实现类似的并发?

Thanks!!

1 个解决方案

#1


Actually, it is not due to Rails, but to the Rails server you are using. Some are single threaded, and others can be launched as multithreaded. For instance, if you use Phusion passenger, you can configure it to run using several threads and so to improve the concurrency. You should look for Rails "server" comparisons instead of trying to find a solution or a problem with the Rails "framework".

实际上,它不是由于Rails,而是由于你正在使用的Rails服务器。有些是单线程的,有些可以作为多线程启动。例如,如果您使用Phusion乘客,则可以将其配置为使用多个线程运行,以便提高并发性。您应该寻找Rails“服务器”比较,而不是试图找到Rails“框架”的解决方案或问题。

Popular servers are Thin, Unicorn, Puma, Phusion passenger. The default development server is call Webrick.

流行的服务器是Thin,Unicorn,Puma,Phusion乘客。默认开发服务器是Webrick。

There are a lot of other * questions relating to the differences between servers so I think you should look into them.

有很多其他*问题与服务器之间的差异有关,所以我认为你应该研究它们。

#1


Actually, it is not due to Rails, but to the Rails server you are using. Some are single threaded, and others can be launched as multithreaded. For instance, if you use Phusion passenger, you can configure it to run using several threads and so to improve the concurrency. You should look for Rails "server" comparisons instead of trying to find a solution or a problem with the Rails "framework".

实际上,它不是由于Rails,而是由于你正在使用的Rails服务器。有些是单线程的,有些可以作为多线程启动。例如,如果您使用Phusion乘客,则可以将其配置为使用多个线程运行,以便提高并发性。您应该寻找Rails“服务器”比较,而不是试图找到Rails“框架”的解决方案或问题。

Popular servers are Thin, Unicorn, Puma, Phusion passenger. The default development server is call Webrick.

流行的服务器是Thin,Unicorn,Puma,Phusion乘客。默认开发服务器是Webrick。

There are a lot of other * questions relating to the differences between servers so I think you should look into them.

有很多其他*问题与服务器之间的差异有关,所以我认为你应该研究它们。