node.js child process - difference between spawn & fork

前端 未结 3 1304
清歌不尽
清歌不尽 2020-12-02 04:36

This might seem like a basic question, but I could not find any documentation :

What is the difference between forking & spawning a node.js process? I have read

3条回答
  •  星月不相逢
    2020-12-02 05:03

    TLDR

    Spawn
    

    When a spawn is created - It creates a streaming interface between parent and child process.

    streaming interface means - buffering data in binary format in ONE TIME

    Fork
    

    When a fork is created - It creates a communication channel between parent and child process

    communication channel means - messaging

    Difference
    

    Well both look kind of doing same data transfer, Except below difference

    spawn will be useful when you want to do continuous data buffer in binary/encoding format , Eg - Transfer 1gb video file,image,log files in ONE TIME

    fork will be useful when you want to do messaging Eg - JSON or XML data messaging

    Conslusion
    

    spawn should be used for streaming big data/files/images FROM spawn process TO parent process

    fork should be used for doing Json/Xml messaging .

    • Eg suppose 10 fork process are created from parent.
    • and each process performs some operation
    • and each process on completing operation will send message to parent 'process no.4 done','process no.8 done'

提交回复
热议问题