我正在尝试使用群集来利用拥有多核CPU的好处。使用代码:
var cluster = require('cluster');
if (cluster.isMaster) {
for(var i = 0; i < 2; ++i) {
cluster.fork();
}
cluster.on('exit', function (worker) {
console.log('Worker ' + worker.process.pid + ' exitted.');
});
} else {
console.log('Worker ' + cluster.worker.process.pid);
process.exit(0);
}
节点与输出
完美配合Worker 14058
Worker 14064
Worker 14058 exitted.
Worker 14064 exitted.
但是,当我尝试将import
与babel一起使用时,我遇到了问题:
import * as cluster from 'cluster'
if (cluster.isMaster) {
for(let i = 0; i < 2; ++i) {
cluster.fork();
}
cluster.on('exit', (worker) => console.log('Worker ' + worker.process.pid + ' exitted.'));
} else {
console.log('Worker ' + cluster.worker.process.pid);
process.exit(0);
}
节点的输出(在babel之后)是:
2.js:13
cluster.on('exit', function (worker) {
^
TypeError: cluster.on is not a function
at Object.<anonymous> (2.js:13:13)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.runMain (module.js:590:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3
Worker 14140
Worker 14146
这看起来很奇怪。我使用节点v6.4.0
,使用babel 6.11.4 (babel-core 6.13.2)
,.babelrc
的内容为:
{
"presets": ["es2016", "es2015"]
}
有什么想法发生了什么?
答案 0 :(得分:1)
好的,我找出原因,参考:Difference between import X and import * as X in node.js (ES6 / Babel)?
重点是import * as cluster from 'cluster'
改为import cluster from 'cluster'
。
使用import * as cluster from 'cluster'
,可导出的所有内容都会导出到名为cluster
的对象中,并且具有以下结构:
{ domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
Worker:
{ [Function: Worker]
super_:
{ [Function: EventEmitter]
EventEmitter: [Circular],
usingDomains: false,
defaultMaxListeners: 10,
init: [Function],
listenerCount: [Function] } },
isWorker: false,
isMaster: true,
workers: {},
settings: {},
schedulingPolicy: 2,
SCHED_NONE: 1,
SCHED_RR: 2,
setupMaster: [Function],
fork: [Function],
disconnect: [Function],
default:
EventEmitter {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
Worker: { [Function: Worker] super_: [Object] },
isWorker: false,
isMaster: true,
workers: {},
settings: {},
schedulingPolicy: 2,
SCHED_NONE: 1,
SCHED_RR: 2,
setupMaster: [Function],
fork: [Function],
disconnect: [Function] } }
另一方面,当import cluster from 'cluster'
时,cluster
对象是默认导出:
EventEmitter {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
Worker:
{ [Function: Worker]
super_:
{ [Function: EventEmitter]
EventEmitter: [Circular],
usingDomains: false,
defaultMaxListeners: 10,
init: [Function],
listenerCount: [Function] } },
isWorker: false,
isMaster: true,
workers: {},
settings: {},
schedulingPolicy: 2,
SCHED_NONE: 1,
SCHED_RR: 2,
setupMaster: [Function],
fork: [Function],
disconnect: [Function] }
答案 1 :(得分:1)
形成我,我从集群中解构了我所需的东西,一个接一个地导入了它:
import { fork, on, isMaster } from 'cluster';
然后
if (isMaster) {
console.log(`Forking ${numWorkers} workers`);
const workers = [...Array(numWorkers)].map(_ => fork());
on('online', worker => console.log(`Worker ${worker.process.pid} is online`));
}
答案 2 :(得分:0)
require('babel-core/register')({
presets: ['node6']
});
const app = require('./test.js');
app();
将您的代码保存在test.js中并查看魔法