作者OrO3 (OrO3)
看板Ajax
标题[node] 请问有没有办法写一个非阻塞的function
时间Sun Dec 6 22:06:48 2015
node.js采用事件驱动模式
在语言以及标准库当中用了很多非阻塞的function
例如写入档案时
fs.appendFile()
那我想请问,有没有办法自己建立一个这种,非阻塞的function ?
像是叫他算个东西,算完在再丢上来,而不要占用主执行绪之类的
像是
function count(callback){
//算一些很浪费时间的东西
callback(500);
}
console.log("start");
count(function(ans){
console.log("ans:"+ans);
});
console.log("end");
会显示
start
end
ans:500
(没阻塞在count)
而不是
start
ans:500
end
(阻塞在count)
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 114.38.80.3
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Ajax/M.1449410811.A.A16.html
1F:→ mmis1000: 利用 child_process 可以办到 12/06 22:36
2F:→ mmis1000: 可以把需要计算的东西丢到子程序计算 12/06 22:37
3F:→ mmis1000: 不过用node.js跑需要大量计算的东西其实本来就不是很 12/06 22:37
4F:→ mmis1000: 适合 12/06 22:37
5F:→ OrO3: 原来如此 12/06 22:55
6F:推 mrbigmouth: fibers 不过fibers在比较新的node.js版本好像有问题? 12/07 16:56
7F:→ mmis1000: fibers 貌似还是跑在主thread上阿,load太大还是会影响 12/07 22:18
8F:→ mmis1000: 到主程式 12/07 22:18