日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

nodeJs多线程 -- 学习记录

發布時間:2025/7/14 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 nodeJs多线程 -- 学习记录 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Fibers 和 Threads

Fibers 稱纖程,可以理解為協同程序,類似py和lua都有這樣的模型。使用Fibers可以避免對資源的互搶,減少cpu和內存的消耗,但是Fibers并不能夠真正的并行執行,同一時刻只有一個Fibers在執行,如果在其中一個Fibers中執行過多的cpu操作或者寫了個死循環,則整個主程序將卡死住。node中的異步事件循環模型就有點象這個。

Threads 又稱線程,他可以在同一時刻并行的執行,他們共享主進程的內存,在其中某一時刻某一個threads鎖死了,是不會影響主線程以及其他線程的執行。但是為了實現這個模型,我們不得不消耗更多的內存和cpu為線程切換的開銷,同時也存在可能多個線程對同一內存單元進行讀寫而造成程序崩潰的問題。

1. cluster

創始人Ryan Dahl建議,運行多個Nodejs進程,利用某些通信機制來協調各項任務。目前,已經有不少第三方的Node.js多進程支持模塊發布,而NodeJS?0.6.x 以上的版本提供了一個cluster模塊?,允許創建“共享同一個socket”的一組進程,用來分擔負載壓力。本篇文章就基于該cluster模塊來講述Node.js在多核CPU下的編程

A single instance of Node.js runs in a single thread. To take advantage of multi-core systems the user will sometimes want to launch a cluster of Node.js processes to handle the load.

The cluster module allows you to easily create child processes that all share server ports.

?

官網實例

1 const cluster = require('cluster'); 2 const http = require('http'); 3 const numCPUs = require('os').cpus().length; 4 5 if (cluster.isMaster) { 6 // Fork workers. 7 for (var i = 0; i < numCPUs; i++) { 8 cluster.fork(); 9 } 10 11 cluster.on('exit', (worker, code, signal) => { 12 console.log('worker' + worker.id +'died'); 13 }); 14 } else { 15 // Workers can share any TCP connection 16 // In this case it is an HTTP server 17 http.createServer((req, res) => { 18 res.writeHead(200); 19 res.end('hello world\n'); 20 }).listen(8000); 21 }

?這段代碼是有主線程 根據cups數目創建子進程,可以根據cluster.work.id打印出是哪個進程處理該請求。

?

?

2.node-threads-a-gogo

? ?a.安裝 ?

npm install threads_a_gogo
實例
A.-?Here's a program that makes Node's event loop spin freely and as fast as possible: it simply prints a dot to the console in each turn: //cat examples/quickIntro_loop.js
1
(function spinForever () { 2 process.stdout.write("."); 3 process.nextTick(spinForever); 4 })();

?

B.-?Here's another program that adds to the one above a fibonacci(35) call in each turn, a CPU-bound task that takes quite a while to complete and that blocks the event loop making it spin slowly and clumsily. The point is simply to show that you can't put a job like that in the event loop because Node will stop performing properly when its event loop can't spin fast and freely due to a callback/listener/nextTick()ed function that's blocking. 1 //cat examples/quickIntro_blocking.js 2 function fibo (n) { 3 return n > 1 ? fibo(n - 1) + fibo(n - 2) : 1; 4 } 5 6 (function fiboLoop () { 7 process.stdout.write(fibo(35).toString()); 8 process.nextTick(fiboLoop); 9 })(); 10 11 (function spinForever () { 12 process.stdout.write("."); 13 process.nextTick(spinForever); 14 })();

C.-?The program below uses?threads_a_gogo?to run the fibonacci(35) calls in a background thread, so Node's event loop isn't blocked at all and can spin freely again at full speed:

1 1 //cat examples/quickIntro_oneThread.js 2 2 function fibo (n) { 3 3 return n > 1 ? fibo(n - 1) + fibo(n - 2) : 1; 4 4 } 5 5 6 6 function cb (err, data) { 7 7 process.stdout.write(data); 8 8 this.eval('fibo(35)', cb); 9 9 } 10 10 11 11 var thread= require('threads_a_gogo').create(); 12 12 13 13 thread.eval(fibo).eval('fibo(35)', cb); 14 14 15 15 (function spinForever () { 16 16 process.stdout.write("."); 17 17 process.nextTick(spinForever); 18 18 })();

?

D.-?This example is almost identical to the one above, only that it creates 5 threads instead of one, each running a fibonacci(35) in parallel and in parallel too with Node's event loop that keeps spinning happily at full speed in its own thread: //cat examples/quickIntro_fiveThreads.js 1 function fibo (n) { 2 return n > 1 ? fibo(n - 1) + fibo(n - 2) : 1; 3 } 4 5 function cb (err, data) { 6 process.stdout.write(" ["+ this.id+ "]"+ data); 7 this.eval('fibo(35)', cb); 8 } 9 10 var threads_a_gogo= require('threads_a_gogo'); 11 12 threads_a_gogo.create().eval(fibo).eval('fibo(35)', cb); 13 threads_a_gogo.create().eval(fibo).eval('fibo(35)', cb); 14 threads_a_gogo.create().eval(fibo).eval('fibo(35)', cb); 15 threads_a_gogo.create().eval(fibo).eval('fibo(35)', cb); 16 threads_a_gogo.create().eval(fibo).eval('fibo(35)', cb); 17 18 (function spinForever () { 19 process.stdout.write("."); 20 process.nextTick(spinForever); 21 })();

?

?E.-?The next one asks?threads_a_gogo?to create a pool of 10 background threads, instead of creating them manually one by one:

1 //cat examples/multiThread.js 2 function fibo (n) { 3 return n > 1 ? fibo(n - 1) + fibo(n - 2) : 1; 4 } 5 6 var numThreads= 10; 7 var threadPool= require('threads_a_gogo').createPool(numThreads).all.eval(fibo); 8 9 threadPool.all.eval('fibo(35)', function cb (err, data) { 10 process.stdout.write(" ["+ this.id+ "]"+ data); 11 this.eval('fibo(35)', cb); 12 }); 13 14 (function spinForever () { 15 process.stdout.write("."); 16 process.nextTick(spinForever); 17 })();

?

F.-?This is a demo of the?threads_a_gogo?eventEmitter API, using one thread:

1 //cat examples/quickIntro_oneThreadEvented.js 2 var thread= require('threads_a_gogo').create(); 3 thread.load(__dirname + '/quickIntro_evented_childThreadCode.js'); 4 5 /* 6 This is the code that's .load()ed into the child/background thread: 7 8 function fibo (n) { 9 return n > 1 ? fibo(n - 1) + fibo(n - 2) : 1; 10 } 11 12 thread.on('giveMeTheFibo', function onGiveMeTheFibo (data) { 13 this.emit('theFiboIs', fibo(+data)); //Emits 'theFiboIs' in the parent/main thread. 14 }); 15 16 */ 17 18 //Emit 'giveMeTheFibo' in the child/background thread. 19 thread.emit('giveMeTheFibo', 35); 20 21 //Listener for the 'theFiboIs' events emitted by the child/background thread. 22 thread.on('theFiboIs', function cb (data) { 23 process.stdout.write(data); 24 this.emit('giveMeTheFibo', 35); 25 }); 26 27 (function spinForever () { 28 process.stdout.write("."); 29 process.nextTick(spinForever); 30 })();

G.-?This is a demo of the?threads_a_gogo?eventEmitter API, using a pool of threads:

?

1 //cat examples/quickIntro_multiThreadEvented.js 2 var numThreads= 10; 3 var threadPool= require('threads_a_gogo').createPool(numThreads); 4 threadPool.load(__dirname + '/quickIntro_evented_childThreadCode.js'); 5 6 /* 7 This is the code that's .load()ed into the child/background threads: 8 9 function fibo (n) { 10 return n > 1 ? fibo(n - 1) + fibo(n - 2) : 1; 11 } 12 13 thread.on('giveMeTheFibo', function onGiveMeTheFibo (data) { 14 this.emit('theFiboIs', fibo(+data)); //Emits 'theFiboIs' in the parent/main thread. 15 }); 16 17 */ 18 19 //Emit 'giveMeTheFibo' in all the child/background threads. 20 threadPool.all.emit('giveMeTheFibo', 35); 21 22 //Listener for the 'theFiboIs' events emitted by the child/background threads. 23 threadPool.on('theFiboIs', function cb (data) { 24 process.stdout.write(" ["+ this.id+ "]"+ data); 25 this.emit('giveMeTheFibo', 35); 26 }); 27 28 (function spinForever () { 29 process.stdout.write("."); 30 process.nextTick(spinForever); 31 })();

?

轉載于:https://www.cnblogs.com/laien/p/5462688.html

總結

以上是生活随笔為你收集整理的nodeJs多线程 -- 学习记录的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产亚洲成人av | 98av视频 | 麻豆视频传媒入口 | 一区二区少妇 | 中文字幕一区二区人妻视频 | 亚洲一区二区中文 | 欧美成人午夜精品久久久 | 超碰91在线 | 在线精品一区二区 | 国产免费91 | 中国黄色一级视频 | 伊人青青草原 | 久久精品123 | 午夜成年视频 | 色欧美亚洲| 波多野吉衣一区二区 | 色日韩 | 成人软件在线观看 | 日韩精品国产一区 | 亚洲AV无码成人精品一区 | 欧美操女人| 黄色一级大片在线免费看产 | 91丨porny丨成人蝌蚪 | 97超碰在线免费观看 | 亚洲精品乱码久久久久久久久久久久 | 播放美国生活大片 | 欧美影院久久 | 搞逼综合网 | 国产中文字幕免费 | 夜操操 | 亚洲第二页| 中文资源在线播放 | 亚洲一区欧洲二区 | 精品国产一区二区三区久久狼黑人 | 国产精品午夜福利视频234区 | 欧美成年人视频在线观看 | 久久久久99精品成人片 | 免费一区视频 | 免费成人黄色网 | 欧美特黄一级视频 | 综合国产精品 | 成人免费视频网站在线看 | 超清av| 无码人妻丰满熟妇啪啪网站 | 日本亚洲一区二区三区 | 999视频在线播放 | 国产99久久久久 | 日韩一区二区高清视频 | 亚洲一区二区三区麻豆 | 字幕网av| 玩日本老头很兴奋xxxx | 国产精品久久久久久久专区 | av免费资源 | 欧美精品三区 | 久久精品免费在线 | 亚洲天堂中文 | 狠狠干2024 | www.亚洲天堂| 国产91在线观看 | 亚洲日本三级 | 青草青视频 | 欧洲精品视频在线 | 特级淫片裸体免费看 | www.色99| 黄色在线播放视频 | av手机免费观看 | 日韩欧美精品一区二区 | 农民工hdxxxx性中国 | www狠狠操| 高清一区二区三区四区五区 | 久久国产色av免费观看 | 不卡在线一区二区 | av在观看| 玖玖久久 | 午夜伦理剧场 | 美女扒开腿让男人捅 | 不卡的av片 | 亚洲丝袜视频 | 欧美激情视频一区 | 欧美日韩一区二区三区不卡视频 | 国产一区二区在线观看视频 | 最好看的mv中文字幕国语电影 | 日韩少妇一区 | 成年人黄色录像 | 日本成人午夜视频 | 中文字幕精 | a级片在线免费看 | 日韩欧美中文在线 | 日日摸天天添天天添破 | 亚洲色图丝袜 | 久草精品在线观看视频 | 国产日韩欧美视频在线观看 | 在线免费看污网站 | www.色com| 亚洲精品视频91 | 看特级黄色片 | 丁香激情网 | 中文字幕第七页 | 黄色大尺度视频 |