最近在写一些奇怪的东西的时候,发现大佬们用go或者其他语言实现的并发任务用了thread.sleep让主进程暂停。
回头一想,妈个鸡我要复制粘贴到node一直循环不合适啊,我也需要暂停来着!
怎么办??
抓了脑袋一会去npm上找了下相关的包,发现有个叫thread-sleep的包,下载量还挺高。
抱着好奇心去看了下源码,又发现源码相当之骚气
'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;
}
黑人问号???
这是什么奇怪的实现。
翻阅node文档发现
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.
???
以上三种同步方法会阻塞nodejs的事件循环,除非创建的子进程执行完了,才会继续执行下面的代码。
thread-sleep包的作者正是利用这一特性实现了sleep功能。叹为观止
所以很多时候我们没办法解决现有问题的原因是对文档不熟么??
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。