rails 如何同时解决多个请求? [英] How rails resolve multi-requests at the same time?

查看:50
本文介绍了rails 如何同时解决多个请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

准备测试:sleep 10 一个动作

测试:在浏览器中打开两个标签页访问动作

Test: Open two tabs in the browser to visit the action

结果:当第二个请求运行时,第一个请求完成并开始渲染视图,但视图仍然是空白的.第二个请求也完成后,两个请求同时完成了视图的渲染.

Result: When the second request is running, the first request finished and began rendering the view, but the view is still blank. After the second request finished too, the two requests finished rendering the view at the same time.

结论:Rails 只是一个实例.一个请求只能在前一个请求完成后进入动作.但是如何解释响应部分呢?为什么多个请求同时渲染完视图?

Conclusion: Rails is just one single instance. One request can only enter the action after the previous requests finish. But how to explain the response part? Why the multi-requests finish rendering the views at the same time?

推荐答案

您使用的是 WEBrick 服务器吗?那一定是因为您的服务器是单线程服务器,并且能够一次完成一个请求(因为只有一个工作线程).现在,如果有多个请求,它会运行请求的操作部分,并在运行视图渲染器之前检查是否有任何待处理的请求.现在,如果排列 10 个请求,它将在实际呈现视图之前首先完成所有请求.当所有这些请求都完成后,视图现在将按顺序呈现.

Are you using a WEBrick server? That must be because your server is a single threaded server and is capable of fulfilling one request at a time (because of the single worker thread). Now in case of multiple requests, it runs the action part of the request and before running the view renderer it checks to see if there are any pending requests. Now if 10 requests are lined up, it would first complete all of them before actually rendering the views. When all of these requests are completed, the views would now be rendered sequentially.

如果需要多线程环境,可以切换到Passenger 或Unicorn 服务器.

You can switch to Passenger or Unicorn server if you want multi-threaded environment.

希望这是有道理的.

这篇关于rails 如何同时解决多个请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆