我一直在为这个概念而苦苦挣扎:
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
, ornapi_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/