When is Node.js Blocking?

I used Node.js for some time, and I just realized that it could be a lock. I just can't wrap my brain around the conditions under which Node.js becomes blocking.

  • So, Node.js is single-threaded because (i) Javascript is and (ii) avoids all multi-threaded traps.
  • To do many things at once, despite being single-threaded, this implements asynchronous execution. So, talking to the database (input / output in the general case) is non-blocking (because it is asynchronous).
  • But all incoming requests do some work (i.e. talking to the database) and all the results of this work, which should return to the client (i.e. send some data), they use this single stream.
  • Node.js uses an "event loop" within a single thread to receive all requests and assign them to non-blocking I / O tasks.

Thus, I / O tasks are not blocked due to asynchronous callbacks, but one thread can be blocked because it is synchronous and because the event loop can be stuck because many complex requests appear at the same time?

  • Did I get it right? I'm probably not because here and here they emphasize that "Node is single-threaded, this means that none of your codes work in parallel." What does this really mean and how does it block Node?
  • So, does the event loop work forever and always look for requests, or does it start execution after it sees a new request?
  • Node Node ?

.

+4
3

-, : node.js . Node libuv, , (, - ), - (, zlib). , crypto ( ) / ( crypto.randomBytes()).

v8 , , , ..

Node .

, :

  • , javascript- , Node. , Node - (I/O) concurrency, () parallelism. Node , cluster, , / , Node - - / ..

  • , , , Node.

  • Node , , - -, - , .

    script (, pi /), , , Node (, ffmpeg , javascript ++ Node Node). , (, HTTP-). , Node , I/O concurrency . script, , / js css script, .

    , script TCP HTTP-, , , , Node , , , , /HTTP- () , .

+7

.

  • , Node.js , . , , CSV json-encode (, , ).

JSON - . , JavaScript. json 20 , CSV (), 20 . - , , / - , "JSON.stringify()".

, - , JSON.stringify, , . .

  1. , , . , , . , .

, JSON 1) . 5 . 5 , . , .

  1. node . , , , node, ?

Node , , , Google. , - , Node.js .

+3

, .

Node.js , , - . javascript. , I/O .

Node.js ...... , .

"", , . -

/ , : c.query( 'SELECT SLEEP(20);', .... - "sleep" , - - ( )

, Node " ".

- ", ". , / , Node.js . / Node.js. , . ( )

, I/O , Node - , -.

, Node .

, , -, Event Loop Event Loop, , . ( )

  • , , - , .

( , )

  1. . , , . , Node . . , , ( ). (FIFO, ?).

    ( "" ), , . , - , JavaScript , . () ( )

, , , , , .

  1. , . , , 1 , 10000 , . Node, . , , , , .

  2. , . , Node , , . ,

    Node , champ, , , .

Node , , . I/O , , , . 5 , .

, , Amazon

... , S3 ( )

( "" ), , . , - , JavaScript , . () ..... - - , . 1 , , , , (, ).

I do not know if I understood this correctly. Please feel free to point out my mistakes and help me get it right.

thank

+1
source

All Articles