node thread.sleep實(shí)現(xiàn)示例
最近在寫一些奇怪的東西的時(shí)候,發(fā)現(xiàn)大佬們用go或者其他語言實(shí)現(xiàn)的并發(fā)任務(wù)用了thread.sleep讓主進(jìn)程暫停。
回頭一想,媽個(gè)雞我要復(fù)制粘貼到node一直循環(huán)不合適啊,我也需要暫停來著!
怎么辦??
抓了腦袋一會(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; }
黑人問號(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í)候我們沒辦法解決現(xiàn)有問題的原因是對(duì)文檔不熟么??
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
阿里大于短信驗(yàn)證碼node koa2的實(shí)現(xiàn)代碼(最新)
本文給大家分享一個(gè)最新版阿里大于短信驗(yàn)證碼node koa2的實(shí)現(xiàn)代碼及注意事項(xiàng),需要的朋友參考下吧2017-09-09nodejs連接mysql數(shù)據(jù)庫簡(jiǎn)單封裝示例-mysql模塊
本篇文章主要介紹了nodejs連接mysql數(shù)據(jù)庫簡(jiǎn)單封裝(mysql模塊),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04node.js對(duì)應(yīng)npm安裝和使用方法教程
這篇文章主要給大家介紹了關(guān)于node.js對(duì)應(yīng)npm安裝和使用方法的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用node.js具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-01-01在Node.js中發(fā)出HTTP請(qǐng)求的 5 種方法
學(xué)習(xí)如何在 Node.js 中發(fā)出 HTTP 請(qǐng)求可能會(huì)讓人感到不知所措,因?yàn)橛袛?shù)十個(gè)可用的庫,每個(gè)解決方案都聲稱比上一個(gè)更高效,在這篇文章中,我們將探討在 Node.js 中發(fā)出 HTTP 請(qǐng)求的五種最流行的方法,并為每種方法提供說明,需要的朋友可以參考下2023-11-11Node.js API詳解之 tty功能與用法實(shí)例分析
這篇文章主要介紹了Node.js API詳解之 tty功能與用法,結(jié)合實(shí)例形式分析了Node.js API中tty的基本功能、用法及終端操作相關(guān)使用技巧,需要的朋友可以參考下2020-04-04三分鐘教你用Node做一個(gè)微信哄女友神器(面向小白)
這篇文章主要介紹了三步教你用Node做一個(gè)微信哄女友神器(面向小白),用node和wechaty微信網(wǎng)頁接口開發(fā)的一款小工具,可以定時(shí)給女朋友發(fā)每天的天氣情況,天氣提醒,每日一句,通過配置機(jī)器人api后還可以實(shí)現(xiàn)微信機(jī)器人自動(dòng)陪女朋友聊天,需要的朋友可以參考下2019-06-06nodejs文件實(shí)現(xiàn)打包成exe, 并設(shè)置開機(jī)自啟動(dòng)的方法詳解(沒有黑窗口)
這篇文章主要介紹了nodejs文件實(shí)現(xiàn)打包成exe, 并設(shè)置開機(jī)自啟動(dòng)的方法,結(jié)合實(shí)例形式分析了node.js使用pkg包實(shí)現(xiàn)生成exe可執(zhí)行文件的相關(guān)操作技巧,需要的朋友可以參考下2023-05-05node使用Mongoose類庫實(shí)現(xiàn)簡(jiǎn)單的增刪改查
Mongoose是在nodejs環(huán)境中對(duì)MongoDB數(shù)據(jù)庫操作的封裝,這篇文章主要介紹了node使用Mongoose類庫實(shí)現(xiàn)簡(jiǎn)單的增刪改查,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11