是否可以在Node.js中分叉javascript函数?

时间:2022-05-19 13:52:11

I try to execute a long run function from my main javascript file. I have 2 cpus so how can I just fork a function ?

我尝试从我的主javascript文件执行长时间运行功能。我有2个cpus所以我怎么才能分叉一个函数?

2 个解决方案

#1


8  

Here is node.js documentation on forking and child processes in node.

这是关于分叉和节点中的子进程的node.js文档。

http://nodejs.org/api/child_process.html

Looks like you might be able to do something like this.

看起来你可能会做这样的事情。

var child = require('child_process').fork('child.js');
child.on("message", function(){});

And you would need the child.js file in this case.

在这种情况下,您需要child.js文件。

#2


1  

For browser-side javascript you would use Web Workers for such a task, the functionality has been ported to NodeJS here Design of Web Workers for NodeJS which wraps child_process API call with the message passing abilities of Web Workers.

对于浏览器端javascript,您可以使用Web Workers执行此类任务,此功能已移植到NodeJS这里为NodeJS设计Web Workers,它使用Web Workers的消息传递功能包装child_process API调用。

#1


8  

Here is node.js documentation on forking and child processes in node.

这是关于分叉和节点中的子进程的node.js文档。

http://nodejs.org/api/child_process.html

Looks like you might be able to do something like this.

看起来你可能会做这样的事情。

var child = require('child_process').fork('child.js');
child.on("message", function(){});

And you would need the child.js file in this case.

在这种情况下,您需要child.js文件。

#2


1  

For browser-side javascript you would use Web Workers for such a task, the functionality has been ported to NodeJS here Design of Web Workers for NodeJS which wraps child_process API call with the message passing abilities of Web Workers.

对于浏览器端javascript,您可以使用Web Workers执行此类任务,此功能已移植到NodeJS这里为NodeJS设计Web Workers,它使用Web Workers的消息传递功能包装child_process API调用。