-
Notifications
You must be signed in to change notification settings - Fork 0
node_child_process
2012 edited this page Jul 20, 2020
·
2 revisions
- exec(command[, options][, callback])
- execFile(file[, args][, options][, callback])
- fork(modulePath[, args][, options])
- spawn(command[, args][, options])
模拟终端处理命令行的功能,便于 js 统一编码,而不再需要写 shell 脚本来执行命令
预习概念
- shell vs process
- 会创建 shell 去运行命令
- 会缓存命令输出的结果数据
- 不适合处理大文件,因为内存有限
- 不允许与主进程进行交互
- 与 child_process.exec() 类似,除了不衍生 shell。 可执行的 file 会被直接衍生为一个新进程,这使得它比 child_process.exec() 更高效。
- spawn 一个新的 node 进程
- 父进程和子进程可以交互通信
- 不会自动退出进程,使用
process.exit()
手动退出
- 不会创建 shell 去运行命令,会 spawn 一个进程去运行命令,因此比 exec 轻量
- 使用 stream
- 适合处理大文件
- 完成时自动退出进程
每个方法都返回 ChildProcess
实例。 这些对象都实现了 EventEmitter
接口,允许父进程注册监听器函数,在子进程生命周期期间,当特定的事件发生时会调用这些函数。
child_process.exec() 和 child_process.execFile() 可以额外指定 callback 函数,当子进程结束时会被调用。
ps
- windows 环境中,不能创建 shell 的函数都不能执行,如 execFile 不能在 windows 环境中使用
- 不要传入不安全的命令到 shell 进程中
JS
CSS
- float
- BFC
- position
- flex
- grid
DOM
- DOM
- how browser works
Node.js
react
- 生命周期
- setState
- hook
git
- git 基础命令
- git rebase 理解
- git bisect
- git commit
- git hook
设计模式
- 策略模式
- ...
TCP/IP 协议
- HTTP/1.x
- HTTP/2
- SSL/TLS
- TCP
- DNS
- HTTP Cache
- CORS
- http status code
linux(shell)知识点