node thread.sleep實(shí)現(xiàn)示例
最近在寫(xiě)一些奇怪的東西的時(shí)候,發(fā)現(xiàn)大佬們用go或者其他語(yǔ)言實(shí)現(xiàn)的并發(fā)任務(wù)用了thread.sleep讓主進(jìn)程暫停。
回頭一想,媽個(gè)雞我要復(fù)制粘貼到node一直循環(huán)不合適啊,我也需要暫停來(lái)著!
怎么辦??
抓了腦袋一會(huì)去npm上找了下相關(guān)的包,發(fā)現(xiàn)有個(gè)叫thread-sleep的包,下載量還挺高。
抱著好奇心去看了下源碼,又發(fā)現(xiàn)源碼相當(dāng)之騷氣
'use strict';
var childProcess = require('child_process');
var nodeBin = process.argv[0];
module.exports = sleep;
function sleep(milliseconds) {
var start = Date.now();
if (milliseconds !== Math.floor(milliseconds)) {
throw new TypeError('sleep only accepts an integer number of milliseconds');
} else if (milliseconds < 0) {
throw new RangeError('sleep only accepts a positive number of milliseconds');
} else if (milliseconds !== (milliseconds | 0)) {
throw new RangeError('sleep duration out of range')
}
milliseconds = milliseconds | 0;
var shouldEnd = start + milliseconds;
try {
childProcess.execFileSync(nodeBin, [ '-e',
'setTimeout(function() {}, ' + shouldEnd + ' - Date.now());'
], {
timeout: milliseconds,
});
} catch (ex) {
if (ex.code !== 'ETIMEDOUT') {
throw ex;
}
}
var end = Date.now();
return end - start;
}
黑人問(wèn)號(hào)???
這是什么奇怪的實(shí)現(xiàn)。
翻閱node文檔發(fā)現(xiàn)
Synchronous Process Creation#
The child_process.spawnSync(),
child_process.execSync(), and child_process.execFileSync() methods are synchronous and WILL block the Node.js event loop,
pausing execution of any additional code until the spawned process exits.Blocking calls like these are mostly useful for simplifying general-purpose scripting tasks and for simplifying the loading/processing of application configuration at startup.
???
以上三種同步方法會(huì)阻塞nodejs的事件循環(huán),除非創(chuàng)建的子進(jìn)程執(zhí)行完了,才會(huì)繼續(xù)執(zhí)行下面的代碼。
thread-sleep包的作者正是利用這一特性實(shí)現(xiàn)了sleep功能。嘆為觀止

所以很多時(shí)候我們沒(méi)辦法解決現(xiàn)有問(wèn)題的原因是對(duì)文檔不熟么??
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺談Node Inspector 代理實(shí)現(xiàn)
這篇文章主要介紹了淺談Node Inspector 代理實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
Node.js 回調(diào)函數(shù)實(shí)例詳解
這篇文章主要介紹了Node.js 回調(diào)函數(shù)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-07-07
node+axios實(shí)現(xiàn)服務(wù)端文件上傳示例
這篇文章主要介紹了node+axios實(shí)現(xiàn)服務(wù)端文件上傳示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
node.js中的定時(shí)器nextTick()和setImmediate()區(qū)別分析
本文介紹了node.js中的定時(shí)器nextTick()和setImmediate()的區(qū)別分析,非常的不錯(cuò),這里推薦給大家。2014-11-11
使用Node.js實(shí)現(xiàn)簡(jiǎn)易MVC框架的方法
下面小編就為大家?guī)?lái)一篇使用Node.js實(shí)現(xiàn)簡(jiǎn)易MVC框架的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08
Nodejs進(jìn)階:核心模塊net入門(mén)學(xué)習(xí)與實(shí)例講解
本篇文章主要是介紹了Nodejs之NET模塊,net模塊是同樣是nodejs的核心模塊,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-11-11
在Linux系統(tǒng)上更新Node.js到最新版本的3種方法小結(jié)
這篇文章主要介紹了在Linux系統(tǒng)上更新Node.js到最新版本的3種方法,使用NVM,使用NPM,用二進(jìn)制包更新Node.js,文中有詳解更新方法,需要的朋友可以參考下2023-09-09
node.js WEB開(kāi)發(fā)中圖片驗(yàn)證碼的實(shí)現(xiàn)方法
這篇文章主要介紹了node.js WEB開(kāi)發(fā)中圖片驗(yàn)證碼的實(shí)現(xiàn)方法,使用ccap模塊實(shí)現(xiàn),需要的朋友可以參考下2014-06-06

