NodeJS 子进程对主进程的引用
NodeJS Child Process reference to main process
我正在努力在 NodeJS 中创建单独的进程。
如果我要分叉一个子进程并向它发送一个对象,该对象是否会通过引用传递?因此,如果我要在该对象的子进程中编辑一个变量,它也会在主进程中发生变化吗?还是做这样的事情的唯一方法是向主进程发送一条消息,告诉它要将变量更改为什么?
Node.js 文档清楚地说:
It is important to keep in mind that spawned Node.js child processes
are independent of the parent with exception of the IPC communication
channel that is established between the two. Each process has its own
memory, with their own V8 instances.
但是Linux里面有Shared Memory的概念。这是一个名为 'EMS.js' 的库,允许在 Node.js.
中使用它
这是一个例子。我在child和parent中设置数字,然后对应读取。
master.js:
const {fork} = require('child_process');
const ems = require('ems')(1);
const _ = require('lodash');
const shared = ems.new({
filename: '/tmp/shared.ems',
heapSize: 10000,
dimensions: 100,
useMap: true,
//Notice this option here
useExisting: false,
ES6proxies: true
});
setInterval(function () {
shared.parentProperty = `Parent sets: ${_.random(1, 100)}`;
console.log(shared.childProperty);
}, 500);
//I inherit stdio here to see the output of both processes in console
let child1 = fork('./slave-1.js', {stdio: 'inherit'});
奴隶-1.js:
const _ = require('lodash');
console.log('Slave started');
const ems = require('ems')(1);
const shared = ems.new({
filename: '/tmp/shared.ems',
dimensions: 100,
useMap: true,
useExisting: true,
ES6proxies: true
});
setInterval(function () {
console.log(shared.parentProperty);
shared.childProperty = `Child sets: ${_.random(1, 100)}`;
}, 500);
还要检查 the docs,因为有很多选项 + 很多东西,例如障碍等。另外要注意堆大小和维度选项,不要 运行 内存不足。
输出:
Slave started
undefined
Parent sets: 52
Child sets: 52
...
我正在努力在 NodeJS 中创建单独的进程。
如果我要分叉一个子进程并向它发送一个对象,该对象是否会通过引用传递?因此,如果我要在该对象的子进程中编辑一个变量,它也会在主进程中发生变化吗?还是做这样的事情的唯一方法是向主进程发送一条消息,告诉它要将变量更改为什么?
Node.js 文档清楚地说:
It is important to keep in mind that spawned Node.js child processes are independent of the parent with exception of the IPC communication channel that is established between the two. Each process has its own memory, with their own V8 instances.
但是Linux里面有Shared Memory的概念。这是一个名为 'EMS.js' 的库,允许在 Node.js.
中使用它这是一个例子。我在child和parent中设置数字,然后对应读取。
master.js:
const {fork} = require('child_process');
const ems = require('ems')(1);
const _ = require('lodash');
const shared = ems.new({
filename: '/tmp/shared.ems',
heapSize: 10000,
dimensions: 100,
useMap: true,
//Notice this option here
useExisting: false,
ES6proxies: true
});
setInterval(function () {
shared.parentProperty = `Parent sets: ${_.random(1, 100)}`;
console.log(shared.childProperty);
}, 500);
//I inherit stdio here to see the output of both processes in console
let child1 = fork('./slave-1.js', {stdio: 'inherit'});
奴隶-1.js:
const _ = require('lodash');
console.log('Slave started');
const ems = require('ems')(1);
const shared = ems.new({
filename: '/tmp/shared.ems',
dimensions: 100,
useMap: true,
useExisting: true,
ES6proxies: true
});
setInterval(function () {
console.log(shared.parentProperty);
shared.childProperty = `Child sets: ${_.random(1, 100)}`;
}, 500);
还要检查 the docs,因为有很多选项 + 很多东西,例如障碍等。另外要注意堆大小和维度选项,不要 运行 内存不足。
输出:
Slave started
undefined
Parent sets: 52
Child sets: 52
...