Giter Club home page Giter Club logo

learn-nodejs's Introduction

Node.js 多进程之间如何通信

  • 由于 nodejs 是单线程的,如果遇到 cpu 密集请求的时候,比如有长时间运行的计算(大循环等),就会阻塞后面所有的请求,这个时候就需要开启多进程。
  • example

spawn

  • options.stdio
    • 'pipe': equivalent to ['pipe', 'pipe', 'pipe'] (the default)
    • 'overlapped': equivalent to ['overlapped', 'overlapped', 'overlapped']
    • 'ignore': equivalent to ['ignore', 'ignore', 'ignore']
    • 'inherit': equivalent to ['inherit', 'inherit', 'inherit']

fork

  • 默认调用 node 命令,通过 ipc 进行通信

daemon 进程

  • options.detached
    • 将 options.detached 设置为 true 可以使子进程在父进程退出后继续运行。
    • 默认情况下,父进程将等待分离的子进程退出.
    • subprocess.unref()方法 允许父进程独立于子进程退出,除非子进程和父进程之间建立了 IPC 通道。

execFile and exec

  • 输出子进程的全部输出,但子进程输出数据比较大的时候,我们也不可能无限制的让它输出,这样可能会导致内存溢出,所以会有 maxBuffer 的参数进行限制(1048576)。
  • execFile 并没有衍生一个 shell,所以不能用来执行 shell 命令,如果需要执行 shell 命令,可以使用 exec。

cluster

  • 多个进程里开启同一个服务,充分使用多核 CPU ,实现负载均衡的一个效果

插件推荐: GitHub Copilot

learn-nodejs's People

Contributors

xsahxl avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.