In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what are the similarities between spawn and exec". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what are the similarities between spawn and exec"?
Node.js, which handles concurrency based on event drivers, runs in single-threaded mode itself. Node.js starts the child process through child_process to execute the specified program. It mainly includes 4 asynchronous process functions (spawn,exec,execFile,fork) and 3 synchronous process functions (spawnSync,execFileSync,execSync). Generally speaking, we usually use spawn and exec. Among them, the asynchronous process function spawn is the most basic function for creating child processes, and the other three asynchronous functions encapsulate spawn to varying degrees. Spawn can only run specified programs, parameters need to be given in the list, and exec can run complex commands directly.
By default, pipes for and are established between the parent Node.js process and the derived child process. These pipes have limited (platform-related) capacity. If the child process writes stdout that exceeds this limit without capturing the output, the child process will block waiting for the pipeline buffer to accept more data. This is the same as the behavior of pipes in shell. If no output is required, the child process is derived asynchronously using the method without blocking the Node.js event loop. Methods, on the other hand, provide equivalent functionality in a synchronous manner, but prevent the event loop until the derived process exits or terminates.
For convenience, the module provides some synchronous and asynchronous alternatives to and. Note that each of these alternatives is based on or implemented.
Derive a shell and run the command in the shell, and pass the and to the callback function when finished Similar to, except that it derives commands directly by default and does not derive shell first Derives a new Node.js process and invokes the specified module by establishing an IPC communication channel that allows cancellation between the parent and child processes The synchronized version of: blocks the Node.js event loop The synchronized version of: blocks the Node.js event loop
For some use cases, such as automated shell scripts, the synchronization method may be more convenient. In most cases, however, the synchronization method has a significant impact on performance because it stops the event loop until the derived process is complete.
Child_process.spawn (command [, args] [, options])
Child_process.exec (command [, options], callback)
Similarities between spawn and exec
1. Both are used to open a child process to execute the specified command.
2. You can customize the running environment of the child process.
3. All return a ChildProcess object, so they can all get the standard input stream, standard output stream and standard error stream of the child process.
The differences between spawn and exec
1. How to accept parameters: spawn uses an array of parameters, while exec is directly followed by the command.
For example, to run a command, the spawn function needs to be written, while when using the exec function, it can be written directly. Exec will parse the Shell syntax first, so it is more convenient to use complex Shell commands, including pipes, redirects, etc., with the exec function.
2. The amount of data that the child process returns to Node: spawn does not limit the amount of data that the child process can return to Node, while exec has a limit of maxBuffer parameter in the options configuration object, and the default is 200K. If this is exceeded, the child process will be killed and an error will be reported: although you can manually increase the maxBuffer parameter, it is not recommended. From this, we can get a glimpse of part of the original intention of Node.js when setting these two API, spawn application to run child processes that return a large amount of data, such as image processing, file reading, and so on. Exec, on the other hand, is used to run child processes that return only a small number of return values, such as a status code.
3. Compared with the spawn method, the exec method provides one more callback function, which can obtain the output of the child process more easily. This is different from getting the output from the stdout or stderr of the returned ChildProcess object by listening for the data event: the data event is triggered as soon as the child process has data and returns the data to Node. On the other hand, the callback function caches the data in memory (the amount of data is less than the maxBuffer parameter), waits for the child process to run, then calls the callback function, and gives the final data to the callback function.
At this point, I believe you have a deeper understanding of "what are the similarities between spawn and exec". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.