NodeJs中的非阻塞方法介紹
更新時間:2012年06月05日 22:41:38 作者:
NodeJs中的非阻塞方法介紹,需要的朋友可以參考下
首先我們利用NodeJs先構建一個基本的服務器。
index.js
var requestHandler = require("./requestHandler");
var server = require("./server");
var route = {
"/hello": requestHandler.hello,
"/upload": requestHandler.upload
};
server.start(route);
server.js
server.js
var http = require("http");
var url = require("url");
exports.start = function(route) {
var server = http.createServer(function(req, res) {
var pathName = url.parse(req.url).pathname;
var handler = route[pathName];
if (handler) {
console.log("Through path:" + pathName + ":" + new Date().getTime());
handler(res);
} else {
res.writeHead(404, {"Content-Type": "text/plain"});
res.end();
}
});
server.listen(8088);
};
requestHandler.js
exports.hello = function(res) {
res.writeHead(200, {"Content-Type": "text/plain"});
res.write("say hello.");
res.end();
};
exports.upload = function(res) {
res.writeHead(200, {"Content-Type": "text/plain"});
res.write("upload");
res.end();
};
在cmd中,鍵入node index.js即可啟動。
但是,上面的代碼是阻塞的。如果在createServer的回調函數(shù)中,有花費長時間的計算。那么會阻塞node.js的事件輪詢。
NodeJS中,他的高效,關鍵在于快速的返回事件循環(huán)。
我們將requestHandler.js改造如下,在這個例子中,由于事件循環(huán)一直被sleep函數(shù)阻塞著,導致createServer的callback無法及時返回。
function sleep(milliSecond) {
var startTime = new Date().getTime();
console.log(startTime);
while(new Date().getTime() <= milliSecond + startTime) {
}
console.log(new Date().getTime());
}
exports.hello = function(res) {
sleep(20000);
res.writeHead(200, {"Content-Type": "text/plain"});
res.write("say hello.");
res.end();
};
exports.upload = function(res) {
res.writeHead(200, {"Content-Type": "text/plain"});
res.write("upload");
res.end();
};
那么先鍵入http://localhost:8088/hello,后鍵入http://localhost:8088/upload。你會發(fā)現(xiàn),upload雖然不需要花費太多時間,但是卻要等到hello完成。
我們試圖找尋異步調用的方法。比如formidable中的上傳,經測試是非阻塞的。查看formidable的源碼,發(fā)現(xiàn)最關鍵的是下面的代碼:
IncomingForm.prototype.parse = function(req, cb) {
this.pause = function() {
try {
req.pause();
} catch (err) {
// the stream was destroyed
if (!this.ended) {
// before it was completed, crash & burn
this._error(err);
}
return false;
}
return true;
};
this.resume = function() {
try {
req.resume();
} catch (err) {
// the stream was destroyed
if (!this.ended) {
// before it was completed, crash & burn
this._error(err);
}
return false;
}
return true;
};
this.writeHeaders(req.headers);
var self = this;
req
.on('error', function(err) {
self._error(err);
})
.on('aborted', function() {
self.emit('aborted');
})
.on('data', function(buffer) {
self.write(buffer);
})
.on('end', function() {
if (self.error) {
return;
}
var err = self._parser.end();
if (err) {
self._error(err);
}
});
if (cb) {
var fields = {}, files = {};
this
.on('field', function(name, value) {
fields[name] = value;
})
.on('file', function(name, file) {
files[name] = file;
})
.on('error', function(err) {
cb(err, fields, files);
})
.on('end', function() {
cb(null, fields, files);
});
}
return this;
};
在parse中,將head信息解析出來這段是阻塞的。但是真正上傳文件卻是在req.on(data)中,是利用了事件驅動,是非阻塞的。也就是說,他的非阻塞模型依賴整個nodeJS事件分派架構。
那么像sleep那樣消耗大量計算,但是又不能依賴nodeJS分派架構的時候怎么辦?
現(xiàn)在介紹一種,類似于html5 WebWorker的方法。
將requestHandler.js改造如下:
var childProcess = require("child_process");
exports.hello = function(res) {
var n = childProcess.fork(__dirname + "/subProcess.js");
n.on('message', function() {
res.writeHead(200, {"Content-Type": "text/plain"});
res.write("say hello.");
res.end();
});
n.send({});
};
exports.upload = function(res) {
res.writeHead(200, {"Content-Type": "text/plain"});
res.write("upload");
res.end();
};
并加入subProcess.js
function sleep(milliSecond) {
var startTime = new Date().getTime();
console.log(startTime);
while(new Date().getTime() <= milliSecond + startTime) {
}
console.log(new Date().getTime());
}
process.on('message', function() {
sleep(20000);
process.send({});
});
測試,當hello還在等待時,upload已經返回。
結語:
大概在最近,我看了博客園上的很多NodeJs文章,大家都認為NodeJS是異步的。但是是何種程度的異步,這個概念就沒有幾篇文章講對了。
其實NodeJS,他是一個雙層的架構。C++,和javascript。并且是單線程的。這點尤其重要。Node其實是C++利用v8調用js命令,為了實現(xiàn)調用順序維護了一個Event序列。因此,在一個js function內部,他的調用絕對會對其他的function產生阻塞。所以,網上所說的process.nextTick和setTimeout等,都不能夠產生新的線程,以保證不被阻塞。他所實現(xiàn)的,不過是Event序列的元素順序問題。 相對于setTimeout,process.nextTick的實現(xiàn)要簡單的多,直接加入Event序列的最頂層(有個啥啥事件)。而setTimeout是增加了一個c++線程,在指定的時間將callback加入Event序列
以Node的file io為例。他的readFile等函數(shù),第二個參數(shù)是一個callback。那么node中第一件事就是記錄下callback,然后調用底層c++,調用c++開始的過程,你可以看成是異步的。因為那已經到了c++,而不是js這塊。所以,exec到callback為止是異步的,http.createServer到觸發(fā)callback為止是異步的。還有很多,比如mysql的調用方法,不知道大家有沒有看過源碼,他就是socket發(fā)送命令,相信這個過程速度非???。然后等待回調的過程Node用c++隱藏了,他也是異步的。
而我這篇文章想說明的是,如果再js端有花費大量時間的運算怎么辦。就用我上面所說的方法,用js打開c++的線程,這個subprocess.js,不在node的event序列內部維護。是新的線程,因此不會阻塞其他的js function
index.js
復制代碼 代碼如下:
var requestHandler = require("./requestHandler");
var server = require("./server");
var route = {
"/hello": requestHandler.hello,
"/upload": requestHandler.upload
};
server.start(route);
server.js
復制代碼 代碼如下:
server.js
復制代碼 代碼如下:
var http = require("http");
var url = require("url");
exports.start = function(route) {
var server = http.createServer(function(req, res) {
var pathName = url.parse(req.url).pathname;
var handler = route[pathName];
if (handler) {
console.log("Through path:" + pathName + ":" + new Date().getTime());
handler(res);
} else {
res.writeHead(404, {"Content-Type": "text/plain"});
res.end();
}
});
server.listen(8088);
};
requestHandler.js
復制代碼 代碼如下:
exports.hello = function(res) {
res.writeHead(200, {"Content-Type": "text/plain"});
res.write("say hello.");
res.end();
};
exports.upload = function(res) {
res.writeHead(200, {"Content-Type": "text/plain"});
res.write("upload");
res.end();
};
在cmd中,鍵入node index.js即可啟動。
但是,上面的代碼是阻塞的。如果在createServer的回調函數(shù)中,有花費長時間的計算。那么會阻塞node.js的事件輪詢。
NodeJS中,他的高效,關鍵在于快速的返回事件循環(huán)。
我們將requestHandler.js改造如下,在這個例子中,由于事件循環(huán)一直被sleep函數(shù)阻塞著,導致createServer的callback無法及時返回。
復制代碼 代碼如下:
function sleep(milliSecond) {
var startTime = new Date().getTime();
console.log(startTime);
while(new Date().getTime() <= milliSecond + startTime) {
}
console.log(new Date().getTime());
}
exports.hello = function(res) {
sleep(20000);
res.writeHead(200, {"Content-Type": "text/plain"});
res.write("say hello.");
res.end();
};
exports.upload = function(res) {
res.writeHead(200, {"Content-Type": "text/plain"});
res.write("upload");
res.end();
};
那么先鍵入http://localhost:8088/hello,后鍵入http://localhost:8088/upload。你會發(fā)現(xiàn),upload雖然不需要花費太多時間,但是卻要等到hello完成。
我們試圖找尋異步調用的方法。比如formidable中的上傳,經測試是非阻塞的。查看formidable的源碼,發(fā)現(xiàn)最關鍵的是下面的代碼:
復制代碼 代碼如下:
IncomingForm.prototype.parse = function(req, cb) {
this.pause = function() {
try {
req.pause();
} catch (err) {
// the stream was destroyed
if (!this.ended) {
// before it was completed, crash & burn
this._error(err);
}
return false;
}
return true;
};
this.resume = function() {
try {
req.resume();
} catch (err) {
// the stream was destroyed
if (!this.ended) {
// before it was completed, crash & burn
this._error(err);
}
return false;
}
return true;
};
this.writeHeaders(req.headers);
var self = this;
req
.on('error', function(err) {
self._error(err);
})
.on('aborted', function() {
self.emit('aborted');
})
.on('data', function(buffer) {
self.write(buffer);
})
.on('end', function() {
if (self.error) {
return;
}
var err = self._parser.end();
if (err) {
self._error(err);
}
});
if (cb) {
var fields = {}, files = {};
this
.on('field', function(name, value) {
fields[name] = value;
})
.on('file', function(name, file) {
files[name] = file;
})
.on('error', function(err) {
cb(err, fields, files);
})
.on('end', function() {
cb(null, fields, files);
});
}
return this;
};
在parse中,將head信息解析出來這段是阻塞的。但是真正上傳文件卻是在req.on(data)中,是利用了事件驅動,是非阻塞的。也就是說,他的非阻塞模型依賴整個nodeJS事件分派架構。
那么像sleep那樣消耗大量計算,但是又不能依賴nodeJS分派架構的時候怎么辦?
現(xiàn)在介紹一種,類似于html5 WebWorker的方法。
將requestHandler.js改造如下:
復制代碼 代碼如下:
var childProcess = require("child_process");
exports.hello = function(res) {
var n = childProcess.fork(__dirname + "/subProcess.js");
n.on('message', function() {
res.writeHead(200, {"Content-Type": "text/plain"});
res.write("say hello.");
res.end();
});
n.send({});
};
exports.upload = function(res) {
res.writeHead(200, {"Content-Type": "text/plain"});
res.write("upload");
res.end();
};
并加入subProcess.js
復制代碼 代碼如下:
function sleep(milliSecond) {
var startTime = new Date().getTime();
console.log(startTime);
while(new Date().getTime() <= milliSecond + startTime) {
}
console.log(new Date().getTime());
}
process.on('message', function() {
sleep(20000);
process.send({});
});
測試,當hello還在等待時,upload已經返回。
結語:
大概在最近,我看了博客園上的很多NodeJs文章,大家都認為NodeJS是異步的。但是是何種程度的異步,這個概念就沒有幾篇文章講對了。
其實NodeJS,他是一個雙層的架構。C++,和javascript。并且是單線程的。這點尤其重要。Node其實是C++利用v8調用js命令,為了實現(xiàn)調用順序維護了一個Event序列。因此,在一個js function內部,他的調用絕對會對其他的function產生阻塞。所以,網上所說的process.nextTick和setTimeout等,都不能夠產生新的線程,以保證不被阻塞。他所實現(xiàn)的,不過是Event序列的元素順序問題。 相對于setTimeout,process.nextTick的實現(xiàn)要簡單的多,直接加入Event序列的最頂層(有個啥啥事件)。而setTimeout是增加了一個c++線程,在指定的時間將callback加入Event序列
以Node的file io為例。他的readFile等函數(shù),第二個參數(shù)是一個callback。那么node中第一件事就是記錄下callback,然后調用底層c++,調用c++開始的過程,你可以看成是異步的。因為那已經到了c++,而不是js這塊。所以,exec到callback為止是異步的,http.createServer到觸發(fā)callback為止是異步的。還有很多,比如mysql的調用方法,不知道大家有沒有看過源碼,他就是socket發(fā)送命令,相信這個過程速度非???。然后等待回調的過程Node用c++隱藏了,他也是異步的。
而我這篇文章想說明的是,如果再js端有花費大量時間的運算怎么辦。就用我上面所說的方法,用js打開c++的線程,這個subprocess.js,不在node的event序列內部維護。是新的線程,因此不會阻塞其他的js function
相關文章
uni-app小程序中父組件和子組件傳值的實現(xiàn)實例
uniapp父子組件引用傳值,和vue的一樣,沒有小程序那樣的麻煩,下面這篇文章主要給大家介紹了關于uni-app小程序中父組件和子組件傳值的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-08-08
前端將dom轉換成圖片的方法(使用dom-to-image)
這篇文章主要介紹了使用輕量級的dom-to-image插件將DOM元素轉換為圖片并下載,該插件支持多種圖片格式,如SVG、PNG、JPEG等,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2025-01-01
CryptoJS中AES實現(xiàn)前后端通用加解密技術
這篇文章主要為大家詳細介紹了CryptoJS中AES實現(xiàn)前后端通用加解密技術,具有一定的參考價值,感興趣的小伙伴們可以參考一下
2018-12-12 
