gpt4 book ai didi

javascript - Node.js 正在阻塞?

转载 作者:可可西里 更新时间:2023-11-01 16:47:23 25 4
gpt4 key购买 nike

我想我刚刚发现了一些关于 node.js 或浏览器的非常奇怪的东西。所以 node.js 应该是非阻塞的,但一个简单的 setTimeout 会阻塞同一个客户端的整个网站。

这是服务器代码:

// Create HTTP Server
var http = require('http');

// Create page number holder
var page = 0;

http.createServer(function (req, res) {
// Increment Page Number
page++;

// Log page load
console.log('LOAD PAGE', page);

// Set response header
res.writeHead(200, {'Content-Type': 'text/plain'});

// Wait 10 seconds and write Hello World message to client.
setTimeout(function(){ res.end('Hello World\n'); }, 10000);

}).listen(5000); // Listen on port 5000

// Log server start
console.log('Server running on port 5000.');

所以我说的是当我尝试打开 http://mysite.com:5000/ 时在带有两个选项卡的 Chrome(版本 25.0.1364.152)中,第一个选项卡必须在 node.js 处理第二个选项卡之前完成。

看起来像这样:

Client: Open Tab1 
> server console: LOAD PAGE 1

Client: Open Tab2
> server console: Nothing happens

Server: Wait 10 seconds
> server console: LOAD PAGE 2

我希望 node.js 立即运行两个请求,否则这不是非阻塞的。我已经使用 Firefox 19 对此进行了测试,因此似乎浏览器对此的行为方式相同。

这是怎么回事?我错过了什么吗? :OOOO

更新

我刚刚在 Safari(6.0.2) 中运行测试并且它工作正常,所以这可能是 Chrome 和 Firefox 以及其他浏览器的问题。

已使用 Node.js 版本 v0.6.17v0.8.21 进行测试

最佳答案

浏览器实现了每个域名的并发连接限制。剪断:

Browsers typically limit the number of concurrent HTTP connections per server to be 2(the term “per server" is used broadly here for the sake of simplicity. For example, it could be multiple physical machines sharing the same public IP.). If a browser session has two HTTP connections already, any further connection requests will have to wait until one of these two HTTP connections is finished. As a result, the browser (or the application) appears to be locked up. Though the "two connection limit" made sense years ago, it is very problematic in today's Ajax enabled "web 2.0" applications.

来源:http://www.openajax.org/runtime/wiki/The_Two_HTTP_Connection_Limit_Issue

Node 没有阻塞,你的浏览器是。我建议使用 apache 基准测试 (ab -c 10 -n 10) 进行测试。

关于javascript - Node.js 正在阻塞?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15229642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com