NodeJS 中的进程与线程
Last updated
Was this helpful?
Last updated
Was this helpful?
什么是进程间通信? 进程间通信(IPC,Inter-Process Communication),指至少两个进程或线程间传送数据或信号的一些技术或方法。
主要的 IPC 方法
文件
A record stored on disk, or a record synthesized on demand by a file server, which can be accessed by multiple processes.
Most operating systems
信号
A system message sent from one process to another, not usually used to transfer data but instead used to remotely command the partnered process.
Most operating systems
套接字
Most operating systems
消息队列
A data stream similar to a socket, but which usually preserves message boundaries. Typically implemented by the operating system, they allow multiple processes to read and write to the message queue without being directly connected to each other.
Most operating systems
管道
A unidirectional data channel utilizing standard input and output. Data written to the write-end of the pipe is buffered by the operating system until it is read from the read-end of the pipe. Two-way communication between processes can be achieved by using two pipes in opposite "directions".
All POSIX systems, Windows
命名管道
A pipe that is treated like a file. Instead of using standard input and output as with an anonymous pipe, processes write to and read from a named pipe as if it were a regular file.
All POSIX systems, Windows, AmigaOS 2.0+
共享内存
Multiple processes are given access to the same block of memory which creates a shared buffer for the processes to communicate with each other.
All POSIX systems, Windows
child_process
模块提供四种方式用以创建子进程:
execFile: 子进程中执行的是非 node 程序,提供一组参数后,执行的结果以回调的形式返回。
exec: 子进程执行的是非 node 程序,传入一串 shell 命令,执行后结果以回调的形式返回,与 execFile 不同的是 exec 可以直接执行一串 shell 命令。
spawn: 子进程中执行的是非 node 程序,提供一组参数后,执行的结果以流的形式返回。
fork: 子进程执行的是 node 程序,提供一组参数后,执行的结果以流的形式返回,与 spawn 不同,fork生成的子进程只能执行 node 应用。
除此之外,child_process
也提供了 execSync
, execFileSync
, spawnSync
三种同步方式执行子进程。
spawn 同样是用于执行非 node 应用,且不能直接执行 shell,与 execFile 相比,spawn 执行应用后的结果并不是执行完成后,一次性的输出的,而是以流的形式输出。对于大批量的数据输出,通过流的形式可以介绍内存的使用。
node 中提供了 fork 方法,通过 fork 方法在单独的进程中执行 node 程序,并且通过父子间的通信,子进程接受父进程的信息,并将执行后的结果返回给父进程。
常见问题
1. 子进程启动失败
问题原因
进程拥有独立的资源,比如内存、端口
根据同一个 server 配置 fork 出来的多个子进程,如果是 http server 服务,占用相同的端口,必然会报错。
解决方案
反向代理
IPC 传递 socket
Cluster
2. 子进程异常退出怎么办
监听子进程退出事件
子进程事件机制
process.on('exit', callback)
退出后重新 refork 子进程
3. 孤儿进程 / 僵尸进程
父进程已退出,子进程还在运行
获取所有子进程
信号机制,守护进程监听 SIGTERM
,利用 process.kill
退出所有子进程
A single instance of Node.js runs in a single thread. To take advantage of multi-core systems, the user will sometimes want to launch a cluster of Node.js processes to handle the load.
The cluster module allows easy creation of child processes that all share server ports.