How to have heavy processing operations done in node.js

后端 未结 3 517
轮回少年
轮回少年 2021-02-04 17:50

I have a heavy data processing operation that I need to get done per 10-12 simulatenous request. I have read that for higher level of concurrency Node.js is a good platform and

相关标签:
3条回答
  • 2021-02-04 18:17

    Yes it will block it, as the callback functions are executed in the main loop. It is only the asynchronously called functions which do not block the loop. It is my understanding that if you want the image processing to execute asynchronously, you will have to use a separate processes to do it.

    Note that you can write your own asynchronous process to handle it. To start you could read the answers to How to write asynchronous functions for Node.js.

    UPDATE

    how do i create a non-blocking asynchronous function in node.js? may also be worth reading. This question is actually referenced in the other one I linked, but I thought I'd include it here to for simplicity.

    0 讨论(0)
  • 2021-02-04 18:26

    When a heavy piece of computation is done in the callback, the event loop would be blocked until the computation is done. That means the callback will block the event loop for the 5 seconds.

    My solution

    It's possible to use a generator function to yield back control to the event loop. I will use a while loop that will run for 3 seconds to act as a long running callback.

    Without a Generator function

    let start = Date.now();
    
    setInterval(() => console.log('resumed'), 500);
    function loop() {
        while ((Date.now() - start) < 3000) { //while the difference between Date.now() and start is less than 3 seconds
            console.log('blocked')
        }
    }
    
    loop();
    

    The output would be:

    // blocked
    // blocked
    //
    //  ... would not return to the event loop while the loop is running
    //
    // blocked
    //...when the loop is over then the setInterval kicks in
    // resumed
    // resumed
    

    With a Generator function

    let gen;
    let start = Date.now();
    
    setInterval(() => console.log('resumed'), 500);
    
    function *loop() {
        while ((Date.now() - start) < 3000) { //while the difference between Date.now() and start is less than 3 seconds
            console.log(yield output())
        }
    }
    
    function output() {
        setTimeout(() => gen.next('blocked'), 500)
    }
    
    gen = loop();
    gen.next();
    

    The output is:

    // resumed
    // blocked
    //...returns control back to the event loop while though the loop is still running
    // resumed
    // blocked
    //...end of the loop
    // resumed
    // resumed
    // resumed
    

    Using javascript generators can help run heavy computational functions that would yield back control to the event loop while it's still computing.

    To know more about the event loop visit https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/function*

    https://davidwalsh.name/es6-generators

    0 讨论(0)
  • 2021-02-04 18:32

    Unfortunately, I don't yet have enough reputation points to comment on Nick's answer, but have you looked into Node's cluster API? It's currently still experimental, but it would allow you to spawn multiple threads.

    0 讨论(0)
提交回复
热议问题