0

我想要一个 node.js 架构,其中有一个主线程创建一个大对象,然后生成多个工作线程/线程来处理该对象。

我尝试在创建时将对象传递给工人,但这非常慢。
我猜是因为对象被序列化然后反序列化并且由于它的大小需要很长时间。

是否可以使用 Node-API 或 Node 插件,在其范围内持久化在 main 中创建的大对象,然后使其可供其他工作人员使用?

我正在尝试创建一些 Node-API 代码以具有存储大对象的“set”方法和用于工作人员获取大对象的“get”方法。它正在编译但无法正常工作。

#include <assert.h>


#define NAPI_EXPERIMENTAL
#include <node_api.h>


static napi_value Set(napi_env env, napi_callback_info info) {
  napi_value to_be_set;
  napi_get_cb_info(env, info, NULL, &to_be_set, NULL, NULL);
  assert(napi_set_instance_data(env, to_be_set, NULL, NULL) == napi_ok);

  return NULL;
}

static napi_value Get(napi_env env, napi_callback_info info) {
  void* to_get;
  assert(napi_get_instance_data(env, (void**)(&to_get)) == napi_ok);

  return to_get;
}

NAPI_MODULE_INIT(/*env, exports*/) {
  void * value = malloc(sizeof(void*));

  napi_property_descriptor bindings[] = {
    {"set", NULL, Set, NULL, NULL, NULL, napi_enumerable, NULL},
    {"get", NULL, Get, NULL, NULL, NULL, napi_enumerable, value}
  };

  assert(napi_define_properties(env,
                                exports,
                                sizeof(bindings) / sizeof(bindings[0]),
                                bindings) == napi_ok);

  return exports;
}
4

0 回答 0