nodejs的child_process
child_process 模块提供了衍生子进程的能力
异步方式:spawn、exec、execFile、fork
同步方式:spawnSync、execSync、execFileSync
说明:
.exec()、.execFile()、.fork() 底层都是通过 .spawn() 实现的
.exec()、execFile() 还提供了回调,当子进程停止的时候执行
.spawnSync()是 .spawn()的同步版 ,将会阻塞 Node.js 事件循环
.execSync() 是 .exec() 的同步版本,将会阻塞 Node.js 事件循环
.execFileSync() 是 .execFile() 的同步版本,将会阻塞 Node.js 事件循环
(1) spawn
使用指定的命令行参数创建新进程
child_process.spawn(command[, args][, options])
command: 要执行的指令
args: Array字符串参数数组
options: 配置项
(2) exec
创建子shell,可以直接执行shell管道命令,有回调
只适用于命令执行结果数据小的情况
child_process.exec(command[, options][, callback])
command: 要执行的指令
options: 配置项
callback:回调
"use strict";
var path = require("path"),
execFile = require("child_process").exec,
fs = require('fs'); module.exports = function convert(source, dest, options) { return new Promise(function(resolve, reject) { var convertPath; if (options && options.path) {
convertPath = options.path + '/Convert';
} else {
convertPath = 'Convert';
} if (!fs.existsSync(source)) {
reject('Unable to open the source file.');
return;
} var args = [source, dest];
if (options && options.args) {
args = args.concat(options.args);
} var cmd = convertPath+" "+args.join(" "); execFile(cmd, function(err, stdout, stderr) {
if (err) {
reject(err);
} else if (stderr.lenght > 0) {
reject(new Error(stderr.toString()));
} else {
resolve();
}
});
});
};
(3)execFile
用于执行文件,不会创建子shell环境
child_process.execFile(file[, args][, options][, callback])
file:要运行的可执行文件的名称或路径
args:字符串参数的列表
options:配置项
callback:回调
exec() 、execFile() 区别:
是否创建了shell
"use strict";
var path = require("path"),
execFile = require("child_process").execFile,
fs = require('fs');
module.exports = function convert(source, dest, options) {
return new Promise(function(resolve, reject) {
var convertPath;
if (options && options.path) {
convertPath = options.path + '/Convert';
} else {
convertPath = 'Convert';
}
if (!fs.existsSync(source)) {
reject('Unable to open the source file.');
return;
}
var args = [source, dest];
//If user supplies any args concat them to the args array
if (options && options.args) {
args = args.concat(options.args);
}
execFile(convertPath, args, {
maxBuffer: 1024 * 2000
}, function(err, stdout, stderr) {
if (err) {
reject(err);
} else if (stderr.lenght > 0) {
reject(new Error(stderr.toString()));
} else {
resolve();
}
});
});
};
(4)fork
spawn方法的一个特例,fork用于执行js文件创建Node.js子进程
fork方式创建的子进程与父进程之间建立了IPC通信管道
常用于父子进程的通信
child_process.fork(modulePath[, args][, options])
modulePath:要在子进程中运行的模块
args:参数
options:配置
nodejs的child_process的更多相关文章
- nodejs的child_process同步异步
nodejs是一种单线程模型,但是,使用nodejs的child_process模块可以实现多进程任务.利用child_process可以创建子进程,实现子进程和主进程之间的通信. nodejs v0 ...
- NodeJs之child_process
一.child_process child_process是NodeJs的重要模块.帮助我们创建多进程任务,更好的利用了计算机的多核性能. 当然也支持线程间的通信. 二.child_process的几 ...
- nodejs(二)child_process模块
1.child_process是Node.js的一个十分重要的模块,通过它可以实现创建多进程,以利用多核计算资源. child_process模块提供了四个创建子进程的函数,分别是spawn,exec ...
- Nodejs进阶:如何玩转子进程(child_process)
本文摘录自个人总结<Nodejs学习笔记>,更多章节及更新,请访问 github主页地址.欢迎加群交流,群号 197339705. 模块概览 在node中,child_process这个模 ...
- NodeJS学习笔记 (16)子进程-child_process(ok)
原文: https://github.com/chyingp/nodejs-learning-guide/blob/master/README.md 自己的跟进学习: 父进程,子进程,线程之间的关系 ...
- Nodejs密集型CPU解决方案
首先说一下nodejs单线程的优势: 高性能,与php相比,避免了频繁创建切换线程的开销,执行更加迅速,资源占用小. 线程安全,不用担心同一变量被多线程读写,造成程序崩溃. 单线程的异步和非阻塞,其实 ...
- nodejs顺序执行shell
最近工作中需要用到nodejs编写脚本来顺序执行自动化测试用例,编写代码如下: var runCommand = function (command){ child_process.exec(comm ...
- 搭建前端私有npm杂记
随着前端队伍越来越壮大,项目间共享代码就变得尤为重要.常用的框架/类库没必要在每个项目都放一份,团队内部产出的公共模块也需要有合理的共享机制.现在,用npm管理前端代码已经是业界趋势.楼主尝试用私有n ...
- 【Grunt】关于Grunt可视化的尝试
使用Grunt遇到的问题? 必须要安装NodeJS 必须安装grunt-cli 需要编写复杂的Gruntfile.js规则 每个项目中必须存在nodejs的grunt模块 不方便管理每一个包含grun ...
随机推荐
- 自己动手搭建经典的3层 Asp.Net MVC
1:IBaseDAL using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expr ...
- 浅谈 Flask 框架
一.框架对比 Django —— 教科书式框架 优势:组件全,功能全,教科书 劣势:占用资源,创建复杂度高 Flask —— 以简单为基准开发,一切从简,能省则省 优势:轻,块 劣势:先天不足,第三方 ...
- 【转】Linux上安装rz和sz命令
简介 lrzsz 官网入口:http://freecode.com/projects/lrzsz/ lrzsz是一个unix通信套件提供的X,Y,和ZModem文件传输协议 windows 需要向ce ...
- elasticsearch 集群、节点、索引、分片、副本概念
原文链接: https://www.jianshu.com/p/297e13045605 集群(cluster): 由一个或多个节点组成, 并通过集群名称与其他集群进行区分 节点(node): 单个 ...
- [TCP/IP] TCP在listen时的参数backlog的意义
linux内核中会维护两个队列: 1)未完成队列:接收到一个SYN建立连接请求,处于SYN_RCVD状态 2)已完成队列:已完成TCP三次握手过程,处于ESTABLISHED状态 3)当有一个S ...
- [PHP] 广度优先搜索匹配网站所有链接
<?php define('PRE_DOMAIN','www'); define('DOMAIN','sina.com.cn'); define('PROTOCOL','https'); def ...
- Swagger从入门到放弃
如何编写基于OpenAPI规范的API文档 简介 Swagger Swagger是一个简单但功能强大的API表达工具.支持的语言种类繁多 使用Swagger生成API,我们可以得到交互式文档,自动生成 ...
- 两种方式实现浅拷贝 for in实现和Object.assign({}, 对象1, 对象2);
浅拷贝只拷贝对象的一层,如果对象的属性还是对象,那么user3和user4这两个对象对应的值都会发生改变 // 拷贝分为浅拷贝和深拷贝. // 浅拷贝的实现 通过for in实现 var user1 ...
- 出现org.springframework.beans.factory.NoSuchBeanDefinitionException 的解决思路
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: privat ...
- day6_面向对象的概念
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019/7/11 16:20 # @Author : 大坏男孩 # @File : d ...