Grasping the Node JS alternative to multithreading

后端 未结 5 1108
慢半拍i
慢半拍i 2020-11-28 18:58

If I understand correctly Node JS is non blocking...so instead of waiting for a response from a database or other process it moved on to something else and checks back later

5条回答
  •  星月不相逢
    2020-11-28 19:36

    Even if this is an old thread, I thought, that I would share with an idea, how to utilize more that one core in Node.JS app. As Nuray Altin have mentioned - JXcore can do that.

    Simple example:

    var method = function () {
        console.log("this is message from thread no", process.threadId);
    };
    
    jxcore.tasks.runOnThread(0, method);
    jxcore.tasks.runOnThread(1, method);
    
    // this is message from thread no 1
    // this is message from thread no 0
    

    By default there are two threads (you can change it with jxcore.tasks.setThreadCount())

    Of course there is much more that you can do with tasks. The docs are here.

    Few articles on that subject:

    • How multithreaded JXcore applications benefit from multiple cores.
    • How to turn your existing application into a multithreaded one with a few lines of code!

提交回复
热议问题