I have this code periodically calls the load function which does very load work taking 10sec. Problem is when load function is being executed, it's blocking the main flow. If I send a simple GET request (like a health check) while load is being executed, the GET call is blocked until the load call is finished.
function setLoadInterval() { var self = this; this.interval = setInterval(function doHeavyWork() { // this takes 10 sec self.load(); self.emit('reloaded'); }, 20000); I tried async.parallel but still the GET call was blocked. I tried setTimeout but got the same result. How do I make load to running on background so that it doesn't block the main flow?
this.interval = setInterval(function doHeavyWork() { async.parallel([function(cb) { self.load(); cb(null); }], function(err) { if (err) { // log error } self.emit('reloaded'); }) }, 20000);
self.load();do that takes 10 seconds? I mean if it can be done in separate process then just use one of fork|spawn|exec this way it won't block the event loop.