Node.js块响应


                    

当我的“长”响应之一以某种方式阻止了另一个AJAX请求时,我感到不愉快。
我同时调用了3种不同的资源:</ p>

 变量列表= ['/ api / filters','/ api / criteria / brands','/ api / criteria / genders']
list.forEach(函数(项目){$。post(项目)})
</ code> </ pre>

在服务器端,我可以在日志文件中看到以下时间:</ p>

  GET / api / filters 304 51ms
GET / api / criteria / genders 200 1ms
GET / api / criteria / brands 200 0ms
</ code> </ pre>

对于我来说,那看起来很酷,但是在浏览器中,图片完全不同。 </ p>

带有google chrome网络标签的图片 </ p>

因此,看起来浏览器首先等待(长请求)答案,然后才接收最后2个结果。 </ p>

这种行为可能是什么原因? </ p>
     </ div>

展开原文

原文

I have unpleasant situation when one of my "long" response in some way blocks another AJAX requests. I call simultaneously 3 different resources:

var list = ['/api/filters','/api/criteria/brands','/api/criteria/genders']  
list.forEach(function(item){$.post(item)})

On server side I could see the following times in logfile:

GET /api/filters 304 51ms
GET /api/criteria/genders 200 1ms
GET /api/criteria/brands 200 0ms

Thats look cool for me, but in browser the picture is absolutely different.

picture with google chrome network tab

So it looks like browser wait for answer on first ( long request ) and only afterwards receive last 2 results.

What could be the reason for this behavior?

2个回答


每个浏览器一次只处理特定数量的同时请求。 如果您同时触发10个ajax请求,则浏览器会将它们放在堆栈上,然后一个接一个地处理。</ p>

您可以在这个问题。</ p>
     </ div>

展开原文

原文

Every browser just handles a specific amount of simultaneous requests at a time. If you fire 10 ajax requests at the same time, the browser put them on a stack and handles one after the other.

You can find more information about the concurrent requests (because that includes images, javascript, etc as well) in browsern in this question.


运行的节点服务器是单线程的,任何使用CPU周期的代码段都会阻塞整个过程。 </ p>

因此,如果 GET / api / filters </ code>执行大量CPU密集型计算,它将阻塞其他所有请求,直到完成为止。 添加一些有关其实际功能的更多信息可以帮助您找到一个更好的答案。</ p>

如果其中有IO操作,请尝试使其异步。 这将允许节点在第一个正在执行IO时为其他URL提供服务。</ p>
     </ div>

展开原文

原文

The node server runs is single threaded and any piece of code that uses CPU cycles blocks the entire process.

As a result, if GET /api/filters does a lot of CPU intensive computations, it will block any other requests till it completes. Adding some more information about what it actually does can help in putting together a better answer.

In case you have IO operations in there, then try to make them asynchronous. That will allow node to serve the other URLs while the first one is doing IO.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐