worker.js

var cluster = require('cluster')
function fibo(n) {
return n == 0 ? 0 : n > 1 ? fibo(n - 1) + fibo(n - 2) : 1
}
console.log(`worker ${cluster.worker.id} start...`)
process.on('message',msg=>{
console.log(` ${cluster.worker.id} receive data is ${msg}`)
var st = Date.now()
console.log(`worker ${cluster.worker.id} start to work`)
var result = fibo(msg)
console.log(`worker ${cluster.worker.id} finish work and using ${Date.now() - st} ms`)
process.send(result)
})

master.js

module.exports = exuteFibo

function exuteFibo() {
return (new Promise((reslove, reject) => {
var cluster = require('cluster')
const numCpus = require('os').cpus().length
var result = []
var workerID = []
cluster.setupMaster({
exec: '../worker.js',
slient: true
})
if (cluster.isMaster) {
var collection = [41, 41, 41, 41]
var st = Date.now()
for (let i = 0; i < numCpus; i++) {
var wk = cluster.fork()
workerID.push(wk.id)
wk.send(collection[i])
}
cluster.on('fork', worker => {
if (workerID.includes(worker.id)) {
console.log(`[master]: fork worker ${worker.id}`)
}
})
cluster.on('exit', (worker, code, signal) => {
console.log(`[master]:worker ${worker.id} died`)
})
var numCount = 0
Object.keys(cluster.workers).forEach(id => {
cluster.workers[id].on('message', msg => {
console.log(`[master] receive message from [worker ${id}]:${msg}`)
result.push(msg)
numCount++
if (numCount == collection.length) {
console.log(`master finish all work adn using ${Date.now() - st} ms`)
workerID.forEach(function(id) {
if (!cluster.workers[id].exitedAfterDisconnect) {
cluster.workers[id].disconnect();
}
})
reslove(result)
}
})
})
}
}))
}

testMaster.js

var exuteFibo = require('./masterFinal.js')
console.log('====start=======')
var st = Date.now()

exuteFibo().then(rs => {
console.log(`Finish all work and using ${Date.now()-st}`)
console.log('####Get result from multipe-processes:' + rs)
})

st = Date.now()

exuteFibo().then(rs => {
console.log(`Finish all work and using ${Date.now()-st}`)
console.log('####Get result from multipe-processes:' + rs)
})

运行结果:

====start=======

[master]: fork worker 1

[master]: fork worker 2

[master]: fork worker 3

[master]: fork worker 4

[master]: fork worker 5

[master]: fork worker 6

[master]: fork worker 7

[master]: fork worker 8

worker 4 start...

worker 1 start...

4 receive data is 41

worker 4 start to work

worker 2 start...

worker 3 start...

1 receive data is 41

worker 1 start to work

2 receive data is 41

worker 2 start to work

worker 5 start...

5 receive data is 41

worker 5 start to work

3 receive data is 41

worker 3 start to work

worker 6 start...

6 receive data is 41

worker 6 start to work

worker 7 start...

7 receive data is 41

worker 7 start to work

worker 8 start...

8 receive data is 41

worker 8 start to work

worker 4 finish work and using 9574 ms

[master] receive message from [worker 4]:165580141

[master] receive message from [worker 4]:165580141

worker 7 finish work and using 9519 ms

[master] receive message from [worker 7]:165580141

worker 6 finish work and using 9652 ms

[master] receive message from [worker 6]:165580141

worker 1 finish work and using 9732 ms

[master] receive message from [worker 1]:165580141

[master] receive message from [worker 1]:165580141

master finish all work adn using 9956 ms

Finish all work and using 9957

####Get result from multipe-processes:165580141,165580141,165580141,165580141

[master]:worker 6 died

[master]:worker 6 died

[master]:worker 7 died

[master]:worker 7 died

worker 2 finish work and using 9778 ms

[master] receive message from [worker 2]:165580141

[master] receive message from [worker 2]:165580141

worker 5 finish work and using 9780 ms

[master] receive message from [worker 5]:165580141

[master]:worker 5 died

[master]:worker 5 died

worker 3 finish work and using 9798 ms

[master] receive message from [worker 3]:165580141

master finish all work adn using 10044 ms

[master] receive message from [worker 3]:165580141

Finish all work and using 10033

####Get result from multipe-processes:165580141,165580141,165580141,165580141

[master]:worker 4 died

[master]:worker 4 died

[master]:worker 1 died

[master]:worker 1 died

[master]:worker 2 died

[master]:worker 2 died

[master]:worker 3 died

[master]:worker 3 died

worker 8 finish work and using 9192 ms

[master] receive message from [worker 8]:165580141

[master]:worker 8 died

[master]:worker 8 died

node cluster模块,仿多线程并发调用,的更多相关文章

  1. node cluster模块的使用和测试

    首先安装async包 用到的有http.cluster包 http和cluster都会node自带的包,无需安装 1:创建cluster.js,代码如下,更具cpu创建多个进程 var cluster ...

  2. Python Socket多线程并发

    1.SocketServer模块编写的TCP服务器端代码 Socketserver原理图 服务端: import SocketServer #导入SocketServer,多线程并发由此类实现 cla ...

  3. Java并发编程 (十) 多线程并发拓展

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一.死锁 1.死锁的定义 所谓的死锁是指两个或两个以上的线程在等待执行的过程中,因为竞争资源而造成的一种 ...

  4. 【nodejs原理&源码赏析(4)】深度剖析cluster模块源码与node.js多进程(上)

    [摘要] 集群管理模块cluster浅析 示例代码托管在:http://www.github.com/dashnowords/blogs 一. 概述 cluster模块是node.js中用于实现和管理 ...

  5. 【nodejs原理&源码赏析(4)】深度剖析cluster模块源码与node.js多进程(上)

    目录 一. 概述 二. 线程与进程 三. cluster模块源码解析 3.1 起步 3.2 入口 3.3 主进程模块master.js 3.4 子进程模块child.js 四. 小结 示例代码托管在: ...

  6. 【nodejs原理&源码赏析(6)】深度剖析cluster模块源码与node.js多进程(下)

    [摘要] cluster模块详解 示例代码托管在:http://www.github.com/dashnowords/blogs 阅读本章需要先阅读本系列前两章内容预热一下. 一. 引言 前两篇博文中 ...

  7. 【nodejs原理&源码赏析(6)】深度剖析cluster模块源码与node.js多进程(下)

    目录 一. 引言 二.server.listen方法 三.cluster._getServer( )方法 四.跨进程通讯工具方法Utils 五.act:queryServer消息 六.轮询调度Roun ...

  8. Node.js的cluster模块——Web后端多进程服务

    众所周知,Node.js是单线程的,一个单独的Node.js进程无法充分利用多核.Node.js从v0.6.0开始,新增cluster模块,让Node.js开发Web服务时,很方便的做到充分利用多核机 ...

  9. C# 防止同时调用=========使用读写锁三行代码简单解决多线程并发的问题

    http://www.jb51.net/article/99718.htm     本文主要介绍了C#使用读写锁三行代码简单解决多线程并发写入文件时提示"文件正在由另一进程使用,因此该进程无 ...

随机推荐

  1. golang 打包,交叉编译,压缩

    打包,压缩 我们的常规打包方式 $ go build Mac下我们用 ls -lh查看,可以看到我们打包出来的可执行文件会比较大,一般只写几行代码就回又3M以上的文件大小了. 我们的带压缩的打包方式 ...

  2. 在 Windows服务器中启用/禁用SMBv1、SMBv2和SMBv3的方法

    本文介绍如何在 SMB 客户端和服务器组件上启用/禁用服务器消息块 SMBv1.SMBv2 和 SMBv3. 注意:建议由专业技术工程师完成以下操作. 禁用 SMBv2 和 SMBv3 的影响 我们建 ...

  3. DDD实践:领域事件

    要求:修改good表,添加 organization 基础定义 用于引发和调度事件的延迟方法 AddDomainEvent Domain\SeedWork\Entity.cs public abstr ...

  4. 迅速上手:使用taro构建微信小程序基础教程

    前言 由于微信小程序在开发上不能安装npm依赖,和开发流程上也饱受诟病:Taro 是由京东·凹凸实验室(aotu.io)倾力打造的 多端开发解决方案,它的api基于react,在本篇文章中主要介绍了使 ...

  5. gradle构建时跳过指定tests

    https://blog.csdn.net/u012672646/article/details/58598532

  6. CND网站加速

    CDN是什么 1-CDN俗称网站加速2-公司一般是购买其他cdn服务商提供的服务3-CDN一般是用来缓存网站的静态资源文件的(css,js,图片,html,htm),浏览器获取某个静态资源是按照就近原 ...

  7. 有关centos7 图形化root用户登录

    好久不用的虚拟机开机后,是图形化登录界面,原来是命令行界面,后来安装和图形化 结果使用我记录的root密码死活登录不了,心想是不是时间久了忘记了root密码 然后开始尝试各种单用户修改root密码,再 ...

  8. libsecp256k1 与 openssl ecdsa

    1. 历史 区块链节点在接收到的用户发送的交易时,首先会验证交易所涉及utxo的可用性.方法是验证用户签名的合法性,涉及的签名算法就是secp256k1,一种椭圆曲线加密算法. 长期以来,实现了该算法 ...

  9. Python_set集合部分功能介绍

    set:无序集合,不能出现重复的元素 set的创建:s1=set() #访问速度快 #解决重复问题 x.add():添加一个新的元素,添加的重复的元素自动过滤掉 x.clear():清空集合 x.di ...

  10. Codeforces 342D Xenia and Dominoes 状压dp

    码就完事了. #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define ...