Skip to content

进程与线程

  • 进程。是操作系统进行资源分配的最小单位(比如打开一个应用程序)
  • 线程。是 CPU 调度的最小单位,不能独立存在,必须依赖于进程

node 相关模块

线程:worker_threads

js
const { Worker } = require('worker_threads')

const worker = new Worker('path/to/foo.js') //接收信息

worker.on('message', (data) => {
  // do sth.
})

// foo.js
const { parentPort } = require('worker_threads')
parentPort.postMessage('sth.')
js
const { fork } = require('child_process')

const worker = fork('path/to/foo.js')

worker.on('message', (data) => {})

//foo.js
process.send('sth.')

参考

Released under the MIT License.