javascript - Is it possible to fork child processes and wait for them to return in NodeJS? - Stack Overflow

I have to do a certain calculation many many times.I want to split it over multiple cores, so I want

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
Add a ment  | 

1 Answer 1

Reset to default 5

If 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).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744892076a4599475.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信