进程
- 一个应用程序,总是通过操作系统启动的,当操作系统启动一个应用程序时,会分配一个拥有独立的可伸缩的内存空间的进程,原则上不受其他进程干扰 - 进程之间是可以通信的,只要两个进程双方遵守一定的协议,比如 ipc - CPU 在不同的进程之间切换执行 - 虽然一个应用程序在启动时只有一个进程,但它在运行的过程中,可以开启新的进程, 进程之间任然保持相对独立 - 如果一个进程是由操作系统开启,称之为主进程 - 如果一个进程 B 由进程 A 开启,则 A 是 B 的父进程,B 是 A 的子进程,子进程会继承父进程的一些信息,但仍然保持相对独立
- nodejs 中开启进程
const childProcess = require("child_parocess") // 导入内置模块 childProcess.exec(在子进程运行的命令,(err,out,stdErr)=> { // 回调函数中可以获取子进程的标准输出,这种数据交互是通过 IPC 完成的,nodejs 已经帮助你完成了处理 // err: 开启进程过程中发生的错误 // out: 子进程输出的正常内容 // stdErr: 子进程输出的错误内容 // 子进程发生任何的错误,绝对不影响父进程,它们的内存空间是完全隔离的 })// 过去,nodejs 没有提供给用户创建线程的接口,只能使用进程的方式 // 过去,nodejs 还提供了 cluster 模块,通过另一种模式创建进程 // 现在,nodejs 已经提供了线程模块,对进程的操作已经很少使用了
线程
- 操作系统启动一个进程(无论是主进程还是子进程),都会自动为它分配一个线程,称之为主线程 - 程序一定在线程上执行 - 主线程在运行的过程中可以创建多个线程,这些线程称之为子线程 - 当操作系统命令 CPUQ 去执行一个进程时,实际上,是在该进程的多个线程中切换执行 - 线程和进程相似,他们都是独立运行,最大的取别在于:线程的内存空间没有隔离,共享进程的内存空间,线程直接的数据不用遵守任何协议们可以随意使用 - 什么是时候需要使用线程 - 使用线程的目的是为了充分使用多核 CPU - 线程执行过程中 尽量不要阻塞 - 最理想的线程效果 - 线程数等于 CPU 的核数 - 线程永不阻塞 - 没有 IO - 只存在大量运算 - 线程相对对立,几乎不使用共享数据 - 线程一般处理 cpu 密集型操作(运算操作) 而 IO 密集型不适合使用线程 适合使用异步 - 注意: 为了避免线程执行过程中共享数据产生的麻烦,nodejs 使用独特的线程机制来尽量规避
进程与线程的区别总结
- 本质区别:进程是操作系统资源分配的基本单位,而线程是处理器任务调度和执行的基本单位。
- 包含关系:一个进程至少有一个线程,线程是进程的一部分,所以线程也被称为轻权进程或者轻量级进程。
- 资源开销:每个进程都有独立的地址空间,进程之间的切换会有较大的开销;线程可以看做轻量级的进程,同一个进程内的线程共享进程的地址空间,每个线程都有自己独立的运行栈和程序计数器,线程之间切换的开销小。
- 影响关系:一个进程崩溃后,在保护模式下其他进程不会被影响,但是一个线程崩溃可能导致整个进程被操作系统杀掉,所以多进程要比多线程健壮。
案例
将1000个随机数(单数长度为千万) 使用多线程进行判断每一个是否为素数,符合条件的返回
index.js
const { Worker } = require('worker_threads') const os = require('os') const arr = require('./number.json') const cpuNum = os.cpus().length const len = Math.ceil(arr.length / cpuNum) let numbers = cpuNum let newArr = [] console.time() for (let i = 0; i < cpuNum; i++) { const data = arr.slice(i * len, (i + 1) * len) const worker = new Worker('./worker.js', { workerData: data, }) worker.on('message', (res) => { newArr.push(...res) numbers-- if(numbers ===0) { console.timeEnd() console.log(newArr); } worker.terminate() }) }
worker.js
const isPrime = require('./isPrime') const { parentPort, workerData, threadId } = require('worker_threads') const name = `线程${threadId}` const newArr = [] for (const n of workerData) { if(isPrime(n)){ newArr.push(n) } } console.log(`${name}处理完成`); parentPort.postMessage(newArr)
number.json
[18863279,39337355,88785013,54756546,25604369,10378764,90772875,97343624,60402268,91863733,74014422,25062252,31777354,68949540,96788621,33916867,76124005,704343,92661013,60521322,49934626,85062196,98613731,49902789,72103014,81682742,79723926,8281743,80177141,86323559,72373398,10219460,96653393,6585800,71820167,62039687,49586812,51102261,87396086,437025,48510797,78052398,65438951,99401481,4411427,37972159,16232517,96906658,31004126,54130020,92941071,69508002,58753309,98269735,22506693,24018025,28813151,19746318,20839241,3128612,16102076,6036927,3874387,48551481,75878569,27800359,64807905,36208197,47880596,14406931,32570255,73355576,90775954,71380351,34373057,52495806,36021584,33123928,10330153,15972273,67899329,8653945,99189999,82448520,79567085,65666576,75633461,93745905,19045020,16219150,66671344,55917597,78877829,64977596,81047339,83070108,92650401,70611864,11716094,95474700,49851865,28573760,63695103,47104627,30494935,88502250,88480881,19818868,57182255,34738397,66787640,20938012,95597212,4221258,91480852,7558178,62727670,5005976,72734917,99197403,86385424,51583595,32402158,1740513,11511782,54306513,41431081,84100463,56013692,97126086,40147162,22865839,85764474,94585658,48460463,84256462,8116003,985786,27670875,80551296,58331049,51683126,45095054,22424480,79118603,13399145,34964605,22547009,74945102,91284065,53714178,24110095,31660126,95780451,8187516,70284883,54138698,61270375,16594067,39179930,25654781,62637383,15193161,80326787,28584821,49039089,80329513,39610157,47135768,45694415,15814770,12787598,35710803,15266625,95504528,34872824,140937,92997809,91302693,64749081,34366632,65651206,86909414,89061016,27620214,9756299,17582982,22148109,2293647,87451695,84001184,66427774,76103972,37337256,37016114,57191545,8030913,93610267,93206621,21523978,79312194,38768143,50954892,44300813,45462781,75525316,95684543,36952584,82647329,8368201,30269295,32824695,206924,98721,75907680,3435668,27248615,23666732,3981893,52876313,77628997,78536326,3535330,340709,96952878,19772217,88063480,61895326,97792238,47015346,39080713,54832000,60695486,99044870,29066993,43168137,6762995,80047401,85884344,42306476,43222544,14541174,453177,51742656,87369994,18051594,99844802,61877683,38830876,60279099,54528813,92652678,31883933,8188100,31395507,77894634,63357083,20986897,35482499,9809749,16728358,88767311,4389509,45253319,46102238,20518793,13551430,79552672,58827751,28238416,95796003,31709372,62921726,69075158,18221548,53230930,9201379,64151063,97739258,2462605,78573813,49960394,57077564,71735112,3416528,25029540,15226261,53127311,22687573,27575742,97636777,11021132,67952886,9949918,943275,80465600,76864183,70534452,40165651,81712019,27036928,14755853,88055878,10063920,44218999,29925789,3052738,2533594,92699824,83548453,60145135,23496859,53710283,1071020,31551562,58387753,66418950,20699632,71488716,52168882,89605285,1421306,72817985,34131502,6526088,34735454,481561,34945821,49173309,9072401,1568298,40417807,81043787,24860484,4731166,94501134,3230666,61871342,48107432,26614148,87567995,32664155,36095499,55677157,12973427,9729668,88111875,67581967,41723181,90510763,76504222,38002959,12552052,37154103,73779460,17219123,49818121,48275782,34650914,5110206,94223089,89261676,400067,59207873,35172173,6444619,92867859,11915932,15009019,1388385,6445589,44953493,81829091,64240527,87680167,15786509,83644355,5447136,46145590,82846160,16868359,94023729,90616515,9829724,38174249,80913184,75629322,84514306,48185078,29026399,92779622,60324481,74209806,48307041,631639,26439240,98512968,60368599,80428679,5879620,39965422,79127450,40015849,88394073,43695472,37995122,2351856,89297157,42066520,30538222,40381434,56122096,36708852,14879357,7086508,62557179,99485496,45237590,69344046,29188496,51516705,14817892,90419474,12163935,59278874,18620641,80301516,2592085,6085726,22086968,3303983,61551495,83189216,9519509,70009499,46170602,24701142,97323318,94707455,24504997,99644377,42301102,10158020,3247111,59467429,43643021,97594738,22072704,36847859,52011397,90959455,5112437,18618557,13442051,79957565,71074811,53417920,51840087,9575642,21620305,59245416,4026861,52384854,90074937,1408843,19720850,67069692,20941408,63001278,92123657,84976334,55942949,5030183,2991515,24225201,60409076,1216157,41298126,83079203,68842530,59859859,4231013,44965265,65450297,92963066,64903067,90910358,77686518,9429302,18090581,45566752,7236066,17284325,19805658,16053369,38065415,37194551,77028929,18944307,49620253,56033381,18674539,84974831,69247511,41379547,50564221,15658149,20942027,88528597,41069790,5907780,45114697,80208482,13002075,83091345,24058315,67424276,17733,24838561,38074795,28690065,13754475,57499980,78540020,95021112,73822361,66072641,95961187,59738025,354736,25137303,45984699,7247116,46797145,62145924,63403502,42877832,10502038,64894006,81608658,5313783,10082921,15933480,14042437,48531943,84938294,42475602,49881491,97391938,42857693,87994700,82709817,97447917,53140612,96985072,30284955,64072180,79933203,51559471,60307178,49391536,11689078,9494572,2416002,51455731,32755170,40836521,26646858,45385186,59153493,90705144,12568525,60222581,43591473,25670322,61483546,25265256,75637515,25645896,63737954,28697486,7222997,53538046,23933930,98542777,50682917,74583491,65306622,18123522,39670669,18168346,42190088,39683916,50601190,65197339,97712884,235836,71004322,74525785,16759039,19017549,9085751,88146767,29018291,70767392,21593055,71343483,67705287,20503487,61447406,67667639,59596892,77771865,33824778,65841880,65955821,1537573,22100002,28689258,46663630,12065936,90570146,66682688,62510239,113535,53882195,99344604,77431472,1975992,80261183,99738660,88644561,16533639,33073587,99573892,80801885,99318335,66618330,21771149,79009179,13701001,28418772,5488865,31466985,61134597,1678654,16755863,83981895,40708834,25156795,5921127,34809447,88758262,79902845,51189776,98798509,56251572,57528328,71253808,28879015,88239015,71233178,74901055,63569768,61439877,88981090,44894886,23024137,39618768,22101740,73363028,18515821,85557893,48118550,78825502,76784973,38457410,25981532,18778730,22983208,54201442,48668119,75311329,3792783,55727616,18474765,42362425,54995021,22284196,89687208,21369134,68246513,49180756,52435962,58315461,18096461,77327066,75460853,79921111,55960351,99308773,27519347,14489419,19102374,21411806,30302567,70300695,72826209,76162955,53250788,42658163,11929952,3644042,19910003,69450265,23024567,6521685,76815520,60793427,82629651,22614031,77962102,5431973,43667455,11346906,67149029,28417813,6245791,33759904,50061987,59924427,80866251,73344658,73802051,11729086,66910434,8018966,92823151,8850776,60968153,52513325,56220443,67881007,94683406,35286045,31420850,90182613,57695164,44871926,15721795,6312397,42368952,6880097,80652090,62774091,21736780,94003849,36538178,13926941,79320343,85240557,42848652,63710726,97815480,11018160,25370796,45980586,2482690,84532018,56335915,25413608,21774827,95751434,838050,25883826,58774660,92388239,73333498,87511643,53669486,85065945,56177912,42480292,36721822,71538740,18482360,91641789,50158158,13393965,19226855,58462900,6848868,39979814,83421535,24237865,31896170,69395373,29295059,60723511,92391529,66022060,87667115,17851107,65971510,1307028,92473927,24154577,99656584,84401191,82201098,34333710,36896644,98205947,49744330,81745151,93733182,25305515,48284051,9444762,85149892,45228275,68108952,16478263,13338263,22671248,88298439,888846,33236357,25497819,41325777,35906963,93664906,90419223,79116478,19702114,20056924,86211274,62258308,96783551,21195237,33266598,83944341,96073561,33813029,40477761,80913376,53148569,43682982,54846649,33103569,34434849,30359104,13445701,84356531,22074494,215505,62175543,57674074,60413462,88449536,70684549,92767163,78733069,43362287,94155458,90510481,9588230,10159372,68897386,97427252,41954769,19895263,14033978,12919857,20342710,91999423,49521119,33182,20063833,78350889,26712545,87818758,11756285,60116033,85216991,36433495,25819203,29618255,54544443,23209106,68736554,58632183,95985970,94628054,15578438,37372345,4448384,15307789,45861117,60333225,2197014,54097558,25204131,99742844,80688529,57803614,10996142,84836785,58792678,13586001,41963277,25110674,48736779,33369653,73156709,37072535,44783515,41269472,64054140,66979353,80349728,66472063,89778369,30684101,23969383,42251063,95804460,8299476,90791416,3589319,46217889,2789586,38891793,71813879,58086052,3013719,61492753,5299837,86299637,978610,18270634,21767677,49649589,89531548,94636790,96656106,29101995,17835027,43697441,95286461,58939850,7483678,74895408,92501595,92929062,10217357,72991470,7683893,63491178,41644523,11924254,43453927,56453464,48294681,23581443,4919663,39434615,86046894,60511626,97627398,89844066,71232338,78605325,63309083,17094162,91331939,29080552,24689514,42263151,86567098,84880857,92631148,37102155,97397164,98935679,53894478,42173661,46731419,95523284,65418626,69946108,30980102,39873774,42524376,50161809,23042724,42388723,66631245,88606943,21130543]
isPrime.js
module.exports = function (n) { if (n < 2) { return false } for (let i = 0; i < n; i++) { if (n & (i === 0)) { return false } } return true }
标签:const,newArr,require,worker,线程,进程 From: https://www.cnblogs.com/rlwan/p/17800050.html