admin管理员组文章数量:1405410
I have to do a certain calculation many many times. I want to split it over multiple cores, so I want to use child_process.fork(), and then pass each child a chunk of the work.
This works fine, except that my main process (the one that does the fork()) just keeps going after fork()ing and has already terminated by the time the children plete their work.
What I really want to do is wait for the children to finish before continuing execution.
I can't find any good way to do this in Node. Any advice?
I have to do a certain calculation many many times. I want to split it over multiple cores, so I want to use child_process.fork(), and then pass each child a chunk of the work.
This works fine, except that my main process (the one that does the fork()) just keeps going after fork()ing and has already terminated by the time the children plete their work.
What I really want to do is wait for the children to finish before continuing execution.
I can't find any good way to do this in Node. Any advice?
Share Improve this question asked Oct 17, 2012 at 20:10 user844942user844942 1431 silver badge5 bronze badges 2- Look into the async module, which can be used for managing and synchronizing callbacks like this. – Joe Commented Oct 17, 2012 at 20:36
-
waitpid
is the system call that you're looking for. Or you can use the close/end events emitted by child process objects. – Mark K Cowan Commented Apr 9, 2017 at 13:02
1 Answer
Reset to default 5If you spawn a new V8 process via .fork()
, it returns a new child
object which implements a munication layer. For instance
var cp = require( 'child_process' ),
proc = cp.fork( '/myfile.js' );
proc.on('message', function( msg ) {
// continue whatever you want here
});
and within /myfile.js
you just dispatch an event when you're done with the work
process.send({ custom: 'message' });
Be aware of the fact that this method indeed spawns a new V8 instance, which eats a good chunk of memory. So you should use that very thoughtfully. Maybe you don't even need to do it that way, maybe there is a more "node like" solution (using process.nextTick
to calculate heavy data).
本文标签: javascriptIs it possible to fork child processes and wait for them to return in NodeJSStack Overflow
版权声明:本文标题:javascript - Is it possible to fork child processes and wait for them to return in NodeJS? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744892075a2630843.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论