Pass large array to node child process

后端 未结 6 1304
予麋鹿
予麋鹿 2021-01-01 10:29

I have complex CPU intensive work I want to do on a large array. Ideally, I\'d like to pass this to the child process.

var spawn = require(\'child_process\'         


        
6条回答
  •  醉话见心
    2021-01-01 11:11

    Why do you want to make a subprocess? The sending of the data across subprocesses is likely to cost more in terms of cpu and realtime than you will save in making the processing happen within the same process.

    Instead, I would suggest that for super efficient coding you consider to do your statistics calculations in a worker thread that runs within the same memory as the nodejs main process.

    You can use the NAN to write C++ code that you can post to a worker thread, and then have that worker thread to post the result and an event back to your nodejs event loop when done.

    The benefit of this is that you don't need extra time to send the data across to a different process, but the downside is that you will write a bit of C++ code for the threaded action, but the NAN extension should take care of most of the difficult task for you.

提交回复
热议问题