Javascript 在NodeJs进程之间使用npm缓存
我正在编写一个nodejs应用程序,我需要分叉主进程(process.fork)。我需要一个模块只需要一次,但这个模块需要两次。示例代码用于说明:Javascript 在NodeJs进程之间使用npm缓存,javascript,node.js,Javascript,Node.js,我正在编写一个nodejs应用程序,我需要分叉主进程(process.fork)。我需要一个模块只需要一次,但这个模块需要两次。示例代码用于说明: a.js var shared = require('./shared'); var cluster = require('cluster'); if(cluster.isMaster){ cluster.fork(); } else { process.exit(0); } console.log('Hello world');
a.js
var shared = require('./shared');
var cluster = require('cluster');
if(cluster.isMaster){
cluster.fork();
} else {
process.exit(0);
}
console.log('Hello world');
module.export = { foo : 'bar' };
shared.js
var shared = require('./shared');
var cluster = require('cluster');
if(cluster.isMaster){
cluster.fork();
} else {
process.exit(0);
}
console.log('Hello world');
module.export = { foo : 'bar' };
shared.js中的代码执行两次。有没有办法让它执行一次?nodejs中每个线程是否有模块缓存
编辑:在我的例子中,
shared
包含一些使用的日志记录配置,并导出一个API以获取日志记录工具。出于保密原因,我不会发布代码。为了让代码正常工作,我们决定让每个线程的职责需要这个模块来配置它。它对master执行一次,对fork执行一次,这就是为什么它会执行两次
如果您只想执行一次,可以这样做:
var cluster = require('cluster');
if (cluster.isMaster) {
var shared = require('./shared');
cluster.fork();
} else {
process.exit(0);
}
const cluster = require('cluster');
if (cluster.isMaster) {
const worker = cluster.fork();
const pid = worker.process.pid;
worker.on('message', function(msg) {
switch (msg.type) {
case 'uncaughtException':
// Handle uncaught exception from worker
// Perhaps fork it again?
break;
case 'console':
// Handle console logging from worker
// Add winston code here
break;
}
});
} else {
process.on("uncaughtException", function (err) {
process.send({ type: "uncaughtException", data: { message: err.message, stack: err.stack } });
process.exit(1);
});
// Override console
console.log = function () {
process.send({ type: "console", method: "log", data: [].slice.call(arguments, 0) });
};
console.error = function () {
process.send({ type: "console", method: "error", data: [].slice.call(arguments, 0) });
};
console.info = function () {
process.send({ type: "console", method: "info", data: [].slice.call(arguments, 0) });
};
console.warn = function () {
process.send({ type: "console", method: "warn", data: [].slice.call(arguments, 0) });
};
console.debug = function () {
process.send({ type: "console", method: "debug", data: [].slice.call(arguments, 0) });
};
}
您还可以在共享
文件中添加cluster.isMaster
检查
关于编辑的补充
处理工作人员日志记录的最简单方法是让主进程处理它。我通常通过从工人向主人发送消息来实现,如下所示:
var cluster = require('cluster');
if (cluster.isMaster) {
var shared = require('./shared');
cluster.fork();
} else {
process.exit(0);
}
const cluster = require('cluster');
if (cluster.isMaster) {
const worker = cluster.fork();
const pid = worker.process.pid;
worker.on('message', function(msg) {
switch (msg.type) {
case 'uncaughtException':
// Handle uncaught exception from worker
// Perhaps fork it again?
break;
case 'console':
// Handle console logging from worker
// Add winston code here
break;
}
});
} else {
process.on("uncaughtException", function (err) {
process.send({ type: "uncaughtException", data: { message: err.message, stack: err.stack } });
process.exit(1);
});
// Override console
console.log = function () {
process.send({ type: "console", method: "log", data: [].slice.call(arguments, 0) });
};
console.error = function () {
process.send({ type: "console", method: "error", data: [].slice.call(arguments, 0) });
};
console.info = function () {
process.send({ type: "console", method: "info", data: [].slice.call(arguments, 0) });
};
console.warn = function () {
process.send({ type: "console", method: "warn", data: [].slice.call(arguments, 0) });
};
console.debug = function () {
process.send({ type: "console", method: "debug", data: [].slice.call(arguments, 0) });
};
}
无论怎样,
NodeJS
都是单线程的,不应该重复两次。。这与cluster.fork()
有关,这会建议它生成另一个实例(但我没有使用cluster),第二次执行会有意义<如果未生成第二个实例,则不会为其定义code>foo。。我看这里没有问题。。它是按设计的..console.log('helloworld'+process.pid)代码>如果你试着这样做,你会发现它们是太独立的进程。现在有两个不同的进程。根据@marcus ekwall的回答,只有当这是主线程时,我才会在shared
中执行一些代码。是的,这只会对主代码执行,但我需要在子进程中访问shared
。无论如何,谢谢你的回答。我将尝试在shared
中使用cluster.isMaster来解决我的问题。@mguimard在这种情况下,您可能会发现这也很有用:@mguimardshared
到底包含什么?请在你的问题中详细说明。但这更像是一个一般性的问题,而不是一个具体的问题。@mguimard好吧,这样的问题没有一般性的答案。有不同的方法,它们并不适合每种情况。我将在回答中添加一些关于集群共享日志记录的评论。