Sushant Gupta
Sushant Gupta

Reputation: 9448

How to have heavy processing operations done in node.js

I have a heavy data processing operation that I need to get done per 10-12 simultaneous requests. I have read that for a higher level of concurrency Node.js is a good platform and it achieves it by having a non-blocking event loop.

What I know is that for having things like querying a database, I can spawn off an event to a separate process (like mongod, mysqld) and then have a callback that will handle the result from that process. Fair enough.

But what if I want to have a heavy piece of computation to be done within a callback? Won't it block other requests until the code in that callback is executed completely? For example, I want to process a high-resolution image, and the code I have is in Javascript itself (no separate process to do image processing).

The way I think of implementing is like

get_image_from_db(image_id, callback(imageBitMap) {
    heavy_operation(imageBitMap); // Can take 5 seconds.
});

Will that heavy_operation stop the node from taking in any request for those 5 seconds? Or am I thinking the wrong way to do such a task? Please guide me, I am a JS newbie.

UPDATE

Or can it be like I could process a partial image and make the event loop go back to take in other callbacks and then return back to process that partial image? (something like prioritizing events).

Upvotes: 8

Views: 9288

Answers (3)

Kayslay
Kayslay

Reputation: 111

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

Upvotes: 4

John Henry
John Henry

Reputation: 3193

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.

Upvotes: 4

Nick Mitchinson
Nick Mitchinson

Reputation: 5480

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.

Upvotes: 5

Related Questions