【问题标题】:node addon with cluster get process id returns same id for all forked process's具有集群获取进程 ID 的节点插件为所有分叉进程返回相同的 ID
【发布时间】:2020-12-12 12:44:56
【问题描述】:

编辑

GetCurrentProcessId() 和 getpid() 返回不同的值...但 boost 不会。

原问题

我正在编写一个节点插件来添加一个本地本地缓存,以便在作为集群运行时与快速服务器一起使用,以拥有一个公共缓存。我在进程之间使用boost的message_queue作为IPC,并且需要唯一标识发送请求的进程。 Boost 提供boost::interprocess::ipcdetail::get_current_process_id 来获取当前进程id,但是在主进程和子进程中返回相同的进程id。我认为我说子进程也有自己的唯一 ID 是正确的。那么这里到底发生了什么:

回购(这是一个最小的可重复性):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;

【问题讨论】:

  • ipcdetail 不是公共 API 的一部分,不应该使用它
  • 现在是 C++ 还是 JS 的问题?还请提取并提供minimal reproducible example

标签: c++ boost node-addon-api


【解决方案1】:

就像我说的,这是一个未记录的实现细节。

但是,如果这确实是您的代码:

std::cout << bip::ipcdetail::get_current_process_id << std::endl;

那么明显的解决方法是调用函数而不是打印它的地址:

std::cout << bip::ipcdetail::get_current_process_id() << std::endl;

注意operator()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-24
    • 2013-12-15
    • 1970-01-01
    • 1970-01-01
    • 2014-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多