node. JS (vii) Sub-process Child_process module

Source: Internet
Author: User

It is well known that node. JS is based on a single-threaded model architecture, designed to deliver efficient CPU utilization, but cannot utilize multiple core CPUs, to address this problem, node. JS provides a child_process module that enables multi-core CPU utilization through multiple processes. The Child_process module provides four functions for creating sub-processes, namely spawn,exec,execfile and fork.

simple usage of 1.spawn functions

The Spawn function publishes a sub-process with the given command and only runs the specified program, and the parameters need to be given in the list. The following example:

  1. var child_process = require(' child_process ');
  2. var child = child_process. Spawn( command );
  3. Child. StdOut. On(' data ', function(data) {
  4. Console. Log(data);
  5. });

By executing the command to get the return result, we can get the standard output stream data.

simple usage of 2.exec functions

exec is also a function to create a child process, unlike the Spawn function it can directly accept a callback function as a parameter, the callback function has three parameters, namely, err, stdout, stderr, the basic use of the following methods:

  1. var child_process = require(' child_process ');
  2. Child_process. EXEC( command , function(err, stdout , stderr ) {
  3. Console. Log( stdout );
  4. });

The EXEC function can directly accept a callback function as a parameter, the callback function has three parameters, namely, err, stdout,stderr, very convenient to use directly,

simple usage of 3.execFile functions

The execfile function is similar to the EXEC function, but the execfile function is more streamlined because it can execute the specified file directly, using the following basic methods:

  1. var child_process = require(' child_process ');
  2. Child_process. ExecFile( file , function(err, stdout , stderr ) {
  3. Console. Log( stdout );
  4. });

ExecFile is similar to the parameters of spawn, and it is necessary to specify the commands and arguments to execute separately, but you can accept a callback function the same as the exec callback function.

simple usage of 4.fork functions

The fork function can run the node. JS module directly, so we can directly manipulate it by specifying the module path. Here's how to use it:

    1. var child_process = require(' child_process ');
    2. Child_process. Fork( modulepath );

This method is a special scenario for spawn (), which is used to derive the node process. In addition to all the methods that an ordinary childprocess instance has, the returned object also has a built-in communication channel.

node. JS (vii) Sub-process Child_process module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.