Node.js process.nextTick still blocking server from getting requests

Lironess

I got this piece of code :

import http from 'http';

function compute() {
  let [sum, i] = [1, 1];
  while (i<1000000000) {
    5*2 
    i++;
  }
  console.log("good");
  process.nextTick(compute);
}

http.createServer((request, response) => {
  response.writeHead(200, {'Content-Type': 'text/plain'});
  response.end('Hello World');
}).listen(5000, '127.0.0.1');

http.request({hostname: '127.0.0.1', port: 5000}, (response) => {
  console.log("here !");
}).end();

compute();

the output for that is always : "good, "good" ... and the HTTP request didn't get called. I thought that process.nextTick should solve that problem, but server is still blocked. Why ? How can I fix it ?

Dan D.

Instead of process.nextTick rather use set setImmediate. Callbacks passed to nextTick are processed before IO callbacks whereas callbacks passed to setImmediate are processed after any that are already pending.

Replace process.nextTick(compute); with setImmediate(compute);.

Moving the CPU work to a child process or worker is also possible. But I won't describe that as my main point was to explain how:

function compute() {
  ...
  console.log("good");
  process.nextTick(compute);
}

would block the HTTP server from handling requests ignoring the while loop which has its own problem.

See setImmediate vs. nextTick for more.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Node.js - process.nextTick and parseJSON

From Dev

Node server response error: process.nextTick(function(){throw err;});

From Dev

How does a Node.js server process requests?

From Dev

MongoDB Database Semaphores and Node.js Process.NextTick()

From Dev

Node.js Uncaught TypeError: callback is not a function in process.nextTick

From Dev

Node.js Uncaught TypeError: callback is not a function in process.nextTick

From Dev

Significance of using setImmediate() and process.nextTick() in Node.js

From Dev

Node.js asynchronous function, process.nextTick()

From Dev

Only allow requests from mobile applications to Node.js server?

From Dev

How to solve trouble by blocking requests for one URI in node.js?

From Dev

node.js + express + threads_a_gogo blocking requests

From Dev

Newcomer to Node, how to understand process.nextTick()

From Dev

How are node Promises getting in between `nextTick` and `setImmediate`?

From Dev

Creating a waiting function using process.nextTick or setImmediate - node.js

From Dev

Understanding Node.js event loop. process.nextTick() never invoked. Why?

From Dev

Node.js — Maximum call stack size exceeded, even with process.nextTick()

From Dev

Node.js and server process

From Dev

MongoDB: \lib\server.js:235 process.nextTick getaddrinfo ENOTFOUND error

From Dev

Cluster process blocking in Node.js/Express application

From Dev

passport.js and process.nextTick in strategy

From Dev

passport.js and process.nextTick in strategy

From Dev

I'm blocking an IP with dos.xml but still get requests from it

From Dev

Gevent async server with blocking requests

From Dev

Node JS - Server doesn't react to requests

From Dev

Node JS Server Requests using buttons

From Dev

Requests to Node.js server timing out due to multiple requests

From Dev

Still getting error connect ECONNREFUSED using node.js MySQL

From Dev

Is grpc server response streaming still blocking?

From Dev

Is making sequential HTTP requests a blocking operation in node?

Related Related

  1. 1

    Node.js - process.nextTick and parseJSON

  2. 2

    Node server response error: process.nextTick(function(){throw err;});

  3. 3

    How does a Node.js server process requests?

  4. 4

    MongoDB Database Semaphores and Node.js Process.NextTick()

  5. 5

    Node.js Uncaught TypeError: callback is not a function in process.nextTick

  6. 6

    Node.js Uncaught TypeError: callback is not a function in process.nextTick

  7. 7

    Significance of using setImmediate() and process.nextTick() in Node.js

  8. 8

    Node.js asynchronous function, process.nextTick()

  9. 9

    Only allow requests from mobile applications to Node.js server?

  10. 10

    How to solve trouble by blocking requests for one URI in node.js?

  11. 11

    node.js + express + threads_a_gogo blocking requests

  12. 12

    Newcomer to Node, how to understand process.nextTick()

  13. 13

    How are node Promises getting in between `nextTick` and `setImmediate`?

  14. 14

    Creating a waiting function using process.nextTick or setImmediate - node.js

  15. 15

    Understanding Node.js event loop. process.nextTick() never invoked. Why?

  16. 16

    Node.js — Maximum call stack size exceeded, even with process.nextTick()

  17. 17

    Node.js and server process

  18. 18

    MongoDB: \lib\server.js:235 process.nextTick getaddrinfo ENOTFOUND error

  19. 19

    Cluster process blocking in Node.js/Express application

  20. 20

    passport.js and process.nextTick in strategy

  21. 21

    passport.js and process.nextTick in strategy

  22. 22

    I'm blocking an IP with dos.xml but still get requests from it

  23. 23

    Gevent async server with blocking requests

  24. 24

    Node JS - Server doesn't react to requests

  25. 25

    Node JS Server Requests using buttons

  26. 26

    Requests to Node.js server timing out due to multiple requests

  27. 27

    Still getting error connect ECONNREFUSED using node.js MySQL

  28. 28

    Is grpc server response streaming still blocking?

  29. 29

    Is making sequential HTTP requests a blocking operation in node?

HotTag

Archive