Node.js Spawn与执行 [英] Node.js Spawn vs. Execute

查看:322
本文介绍了Node.js Spawn与执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在观看我学习Node的在线培训视频中,讲述者说:对于包含大量数据的较长流程而言,生成更好,而对于少量数据,执行则更好."

这是为什么? Node.js中child_process生成和执行函数之间有什么区别?何时知道该使用哪个函数?

解决方案

主要区别在于spawn更适合于具有大量输出的长时间运行的过程. spawn通过子进程流输入/输出. exec缓冲的输出在较小的缓冲区(默认为200K)中.我也知道exec首先生成子外壳,然后尝试执行您的过程.为了避免长话短说,请使用spawn,以防您需要从子进程中传输大量数据,而exec则需要诸如shell管道,重定向之类的功能,甚至需要一次执行多个程序. >

一些有用的链接- DZone Hacksparrow

In an online training video I am watching to learn Node, the narrator says that "spawn is better for longer processes involving large amounts of data, whereas execute is better for short bits of data."

Why is this? What is the difference between the child_process spawn and execute functions in Node.js, and when do I know which one to use?

解决方案

The main difference is the spawn is more suitable for long-running process with huge output. spawn streams input/output with child process. exec buffered output in a small (by default 200K) buffer. Also as I know exec first spawn subshell, then try to execute your process. To cut long story short use spawn in case you need a lot of data streamed from child process and exec if you need such features as shell pipes, redirects or even you need exec more than one program in one time.

Some useful links - DZone Hacksparrow

这篇关于Node.js Spawn与执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆