JavaScript Using worker/background processes in node.js vs async call using background worker,using backgroundworker c#,use backgroundworker class,using

I want to know if there is any benefit in passing off db or other async calls to a worker process or processes. Specifically I'm using heroku and postgres. I've read up a good bit on node.js and how to structure your server so that the event loop isn't blocked and that smart architecture doesn't leave incoming requests hanging longer than 300ms or so.

Say I have the following:

 app.get('/getsomeresults/:query', function(request, response){
    var foo = request.params.query;
    pg.connect(process.env.DATABASE_URL, function(err, client, done) {
            client.query("SELECT * FROM users WHERE cat=$1", [foo], 
            function(err, result){
            //do some stuff with result.rows that may take 1000ms
            response.json({some:data})
            });
    });
 });

Being that postgresql is async by nature is there any real benefit to creating a worker process to handle the processing of the results set from the initial db call?

Answer:1

You don't gain any benefit for running async functions in another process because the real work (running the SQL query) is already running in another process (postgres). Basically, the async/event-oriented design pattern is a lightweight process manager for things that run outside your process.

However, I noticed in your comment that the processing in the callback function does indeed take up a lot of CPU time (if that's really the case). That portion of code does benefit from being run in another process - it frees the main process to accept incoming requests.

There are two ways to structure such code. Either run the async function in a separate process (so that the callback doesn't block) or just run the relevant portion of the callback as a function in a separate process.

Answer:2

I am making a website in Socket.io. But emit method not working in my code. I can't see any errors in my code. Here is my server code. var io = require("socket.io").listen(server); //Some external ...

I am making a website in Socket.io. But emit method not working in my code. I can't see any errors in my code. Here is my server code. var io = require("socket.io").listen(server); //Some external ...

  1. node socket emit
  2. node js socket emit
  3. node js socket emit example
  4. node socket.io emit
  5. node.js socket.emit callback
  6. node js socket emit error
  7. node js net socket emit
  8. node.js socket.io emit
  9. node.js socket.io emit json
  10. node js socket io emit callback
  11. node.js socket.broadcast.emit
  12. socket.emit node.js not working
  13. node.js socket.volatile.emit

I'm trying to play a video that I've CENC-encrypted using MP4box with these options: MP4Box -crypt drm_file_gpac_clear.xml BigBuckBunny.mp4 -out BigBuckBunny_cenc.mp4 Where the XML file specifies ...

I'm trying to play a video that I've CENC-encrypted using MP4box with these options: MP4Box -crypt drm_file_gpac_clear.xml BigBuckBunny.mp4 -out BigBuckBunny_cenc.mp4 Where the XML file specifies ...

I am using axios for AJAX in Vue. In the article written by You, he mentioned that we can set Vue.prototype.$http = axios and I can use this.$http in Vue instance. It works fine. However, if I want ...

I am using axios for AJAX in Vue. In the article written by You, he mentioned that we can set Vue.prototype.$http = axios and I can use this.$http in Vue instance. It works fine. However, if I want ...

  1. create instance axios
  2. create axios instance react
  3. create axios instance vue

I am trying to add 10 number(digit) in each element using map function on button click.I tried like this <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <...

I am trying to add 10 number(digit) in each element using map function on button click.I tried like this <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <...