node.js - 在Node.js中运行Rust程序

标签 node.js rust

我一直在为这个概念而苦苦挣扎:

You can run rust programs in nodejs.


因此我们都知道rust速度快,可以很好地处理并发性,并具有静态内存管理等功能。
所以也许我很愚蠢地理解这个概念。 Nodejs是一个单线程。一切都在事件循环中运行。
因此,如果我们有一个以 rust 编写的程序并且具有多进程卷入等功能,那么这是如何工作的...
如果我们在nodejs内运行程序,那么nodejs的行为是什么?

最佳答案

NodeJs的附加API(N-API)已记录在here中。在“异步线程安全的函数调用”部分中:

JavaScript functions can normally only be called from a native addon's main thread. If an addon creates additional threads, then N-API functions that require a napi_env, napi_value, or napi_ref must not be called from those threads.


When an addon has additional threads and JavaScript functions need to be invoked based on the processing completed by those threads, those threads must communicate with the addon's main thread so that the main thread can invoke the JavaScript function on their behalf. The thread-safe function APIs provide an easy way to do this.


因此,大多数调用在非附加主线程中都是不安全的(基本上是任何会影响Javascript运行时的线程),尽管有一些调用专用于此。

关于node.js - 在Node.js中运行Rust程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64070293/

相关文章:

GDB 在 Rust 可执行文件中找不到调试符号

rust - 如何将 Entry API 与仅在 Entry 为空时才构建的昂贵 key 一起使用?

javascript - 是否可以在 Google Chrome 扩展中使用(或包含)node.js 框架?

node.js - 这个 C# 加密代码在 NodeJS 中会是什么样子?

javascript - 类型 'subtle' 上不存在属性 'typeof webcrypto'

node.js - 将 Rust 与 Neon 绑定(bind)一起使用时,如何获得回溯或以其他方式调试 "failed to initiate panic"?

rust - 阶乘 if 语句期望 '()' 找到整数

rust - 为什么使用不安全代码的 rustdoc 测试会失败,但集成测试中的相同代码会通过?

javascript - 在 Node.js 中使用 .bin 文件内容填充类型化数组

angularjs - 如何将Node JS服务器端数据发送到Angular JS