具有集群获取进程ID的节点附加组件为所有分叉进程的ID返回相同的ID [英] node addon with cluster get process id returns same id for all forked process's
问题描述
GetCurrentProcessId()和getpid()返回不同的值...但是boost没有.
GetCurrentProcessId() and getpid() return different values... but boost does not.
我正在编写一个节点插件,以添加作为集群运行时与Express Server一起使用的本地本机缓存,以具有公共缓存.我正在进程的IPC中使用boost的 message_queue
进行IPC,并且需要唯一地标识发送请求的进程.Boost提供了 boost :: interprocess :: ipcdetail :: get_current_process_id
来获取当前进程ID,但是在主进程和子进程中将返回相同的进程ID.我认为子进程也具有自己的唯一ID是对的.那么这到底是怎么回事:
I am writing a node addon to add a local native cache to be used with an express server when running as a cluster, to have a common cache. I am using boost's message_queue
for IPC between the process's, and need to uniquely identify the process sending the requests. Boost provides boost::interprocess::ipcdetail::get_current_process_id
to get the current process id, but the same process id is returned in the main process and the child process's. I think I am right in saying that child process's have their own unique ID as well. So what exactly is going on here:
回购(可复制性最低): https://github.com/t348575/群集内存共享
Repo (it is a minimal reproducible): https://github.com/t348575/cluster-mem-shared
00007FF95BDC2310
00007FF95BDC2310
00007FF95BDC2310
00007FF95BDC2310
示例js测试文件
require('dotenv').config();
const cluster = require('cluster');
const cache = new (require('./dist/index')).ClusterMemShared(process.env.MODE);
if (cluster.isMaster) {
cluster.fork();
cluster.fork();
cluster.fork();
}
我将其打印在c ++返回js的类的构造函数中
std::cout << bip::ipcdetail::get_current_process_id << std::endl;
推荐答案
就像我说的那样,这是未记录的实现细节.
Like I said, that's an undocumented implementation detail.
但是,如果这确实是您的代码:
However, if this is really your code:
std::cout << bip::ipcdetail::get_current_process_id << std::endl;
然后明显的解决方法是调用该功能,而不是打印其地址:
Then the obvious fix is to call the function instead of printing its address:
std::cout << bip::ipcdetail::get_current_process_id() << std::endl;
请注意 operator()
这篇关于具有集群获取进程ID的节点附加组件为所有分叉进程的ID返回相同的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!