node.js child process - difference between spawn & fork -
this might seem basic question, not find documentation :
what difference between forking & spawning node.js process? have read forking special case of spawning, different use cases / repecussions using each of them?
spawn command designed run system commands. when run spawn, send system command run on own process, not execute further code within node process. can add listeners process have spawned, allow code interact spawned process, no new v8 instance created(unless of course command node command, in case should use fork!) , 1 copy of node module active on processor.
fork special instance of spawn, runs fresh instance of v8 engine. meaning, can create multiple workers, running on exact same node code base, or perhaps different module specific task. useful creating worker pool. while nodes async event model allows single core of machine used efficiently, doesn't allow node process make use of multi core machines. easiest way accomplish run multiple copies of same program, on single processor.
a rule of thumb 1 2 node processes per core, perhaps more machines ram clock/cpu clock ratio, or node processes heavy on i/o , light on cpu work, minimize down time event loop waiting new events. however, latter suggestion micro-optimization, , need careful benchmarking ensure situation suits need many processes/core. can decrease performance spawning many workers machine/scenario.
ultimately use spawn in way did above, sending spawn node command. silly, because fork things optimize process of creating v8 instances. making clear, spawn encompasses fork. fork optimal particular, , useful, use case.
http://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback
Comments
Post a Comment