//计算内存使用率 function calcMem(){ let mem_total = os.totalmem(), mem_free = os.freemem(), mem_used = mem_total - mem_free, mem_ratio = 0; mem_total = (mem_total / (1024 * 1024 * 1024)).toFixed(1); mem_used = (mem_used / (1024 * 1024 * 1024)).toFixed(1); mem_ratio = parseInt(mem_used / mem_total * 100); return { total: mem_total, used: mem_used, ratio: mem_ratio } }
测试worker
‘use strict‘; const fork = require(‘child_process‘).fork, cpus = require(‘os‘).cpus(); //保存被子进程实例数组 var workers = {}; //这里的被子进程理论上可以无限多 var appsPath = [‘./os.js‘,‘./os.js‘,‘./os.js‘]; var createWorker = function(appPath){ //保存fork返回的进程实例 var worker = fork(appPath); //监听子进程exit事件 worker.on(‘exit‘,function(){ console.log(‘worker: ‘ + worker.pid + ‘ exited‘); delete workers[worker.pid]; createWorker(appPath); }); workers[worker.pid] = worker; console.log(‘Create worker: ‘ + worker.pid); } //启动所有子进程 for (var i = appsPath.length - 1; i >= 0; i--) { createWorker(appsPath[i]); }; //父进程退出时杀死所有子进程 process.on(‘exit‘,function(){ for(var pid in workers){ workers[pid].kill(); } })
时间: 2024-10-13 10:34:44