Child_process is a more important module for node, which enables the creation of multi-threading to take advantage of multicore CPUs.
This module provides four functions for creating child processes.
Spawn, exec, execfile, fork.
Spawn is the most primitive function to create a subprocess, and the remaining three is a different encapsulation of the function.
Spawn does not support callback functions.
Both exec and execfile support callback functions. The difference is that the latter does not have to start a separate shell, which is relatively lightweight. Let's take execfile example (open the bat file stored in a fixed position, execute the command line of the file) (Pro-test pass):
1 varChild_process = require (' child_process '));2 //Invoke execution File3 varOpenapp =function(){4Child_process.execfile (' D:/testweb/1.bat ',NULL, {cwd: ' d:/'},function(error,stdout,stderr) {5 if(Error!==NULL) {6Console.log (' EXEC error: ' +error);7 }8 });9 }Ten OneOpenapp ();
The bat file is a fun thing to do, and there will be a post of bat file in the back. ^_^
Resources:
http://my.oschina.net/u/252343/blog/185998
https://www.byvoid.com/zhs/blog/node-child-process-ipc/
Official documents:
Http://nodejs.org/api/child_process.html
node. JS Learning Note (5)-About child_process module