Node.js和C / C ++集成:如何正确实现回调? [英] Node.js and C/C++ integration: how to properly implement callbacks?

查看:175
本文介绍了Node.js和C / C ++集成:如何正确实现回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个C ++扩展以与node.js集成。此扩展将在内部调用一些阻塞调用,因此需要为node.js世界提供一个非阻塞接口。

I am trying to implement a C++ extension to be integrated with node.js. This extension will internally invoke some blocking calls, so it needs to provide a non-blocking interface to the node.js world.

https://nodejs.org/api/addons.html ,有两种方法来实现非阻塞回调:

As specified in https://nodejs.org/api/addons.html, there are two ways to implement non-blocking callbacks:

a)通过使用对JavaScript函数的简单回调。所以我的扩展必须生成一个线程并立即返回,并让该线程调用阻塞代码,然后在返回时调用JavaScript回调。这似乎相对简单的实现。

a) By using a simple callback to a JavaScript function. So my extension would have to spawn a thread and return immediately, and let that thread call the blocking code and then invoke the JavaScript callback upon return. This seems relatively simple to implement.

b)通过使用libuv库,为了,如果我理解正确,post一个事件到node.js事件循环。我没有详细阅读libuv文档,但这似乎很复杂的实现。

b) By using the libuv library in order to, if I understood correctly, post an event to the node.js event loop. I have not read libuv documentation in detail, but this seems quite complex to implement.

我的偏好当然是a),但我不知道什么是。如果从不同的线程调用回调,那么是否有任何问题,从而阻碍了node.js标准方法到非阻塞IO?或者是否需要使用libuv来正确处理我的代码及其阻塞调用的线程?

My preference of course is a), but I have no idea of what are the implications. Is there any problem if the callback is invoked from a different thread, thus cimcurventing the node.js standard approach to non-blocking IO? Or does libuv need to be used to properly handle the threading for my code and its blocking calls?

非常感谢您的帮助。

推荐答案

从不同的线程调用回调不是一个选项,v8不允许。所以你必须去与b。实际上并不难实现。我建议您使用 nan 执行此任务。下面是一个来自docs的例子:

Calling callback from a different thread is not an options, v8 doesn't allow that. So you have to go with b. It's not so hard to implement actually. I recommend to use nan for this task. Here is an example from docs:

class PiWorker : public NanAsyncWorker {
 public:
  PiWorker(NanCallback *callback, int points)
    : NanAsyncWorker(callback), points(points) {}
  ~PiWorker() {}

  // Executed inside the worker-thread.
  // It is not safe to access V8, or V8 data structures
  // here, so everything we need for input and output
  // should go on `this`.
  void Execute () {
    estimate = Estimate(points);
  }

  // Executed when the async work is complete
  // this function will be run inside the main event loop
  // so it is safe to use V8 again
  void HandleOKCallback () {
    NanScope();

    Local<Value> argv[] = {
        NanNull()
      , NanNew<Number>(estimate)
    };

    callback->Call(2, argv);
  };

 private:
  int points;
  double estimate;
};

// Asynchronous access to the `Estimate()` function
NAN_METHOD(CalculateAsync) {
  NanScope();

  int points = args[0]->Uint32Value();
  NanCallback *callback = new NanCallback(args[1].As<Function>());

  NanAsyncQueueWorker(new PiWorker(callback, points));
  NanReturnUndefined();
}

在引擎底下,它将处理使用libuv线程池和调用回调在主线程。

Under the hood it will handle calling your code using libuv thread pool and calling callback on the main thread.

这篇关于Node.js和C / C ++集成:如何正确实现回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆