Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

javascript - Run Arbitrary Code While Waiting For Callback in Node?

I'm a beginner in Node right now. I understand that callbacks prevent Node from blocking while waiting for I/O or some other process, but what is Node doing while waiting for that I/O to finish? One tutorial I read on nodejitsu says, " A callback is a function called at the completion of a given task; this prevents any blocking, and allows other code to be run in the meantime." but what other code is it running? Let's say the other code a given block is dependent on the information being retrieved from the database. Can Node go handle other connections while waiting for the callback or can it only run other code in the current block?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Node works off an event queue. When you start an async operation, that operation proceeds independently from node in the background. Node continues running the rest of your code in the current execution thread until it gets to the end of that execution thread (e.g. everything returns back up to the top of the stack). While node is running a current execution thread, no other threads of execution will run in node (ignoring for the moment generators and fibers). This is why people refer to it as single threaded. It runs a particular thread of execution one at a time sequentially, not multiple threads of execution in parallel. One must finish before the next one runs.

When one thread of execution finishes, if there are other events in the event queue, then node will pop the next event off the queue and run it. Other events in the event queue can be from anything. They can be from timers, from the async event you just start or from async events elsewhere in your program (e.g. other database operations finishing in your specific question). If there are no other events (so thus no code ready to be run), then node just waits until the next event occurs (doing nothing except perhaps garbage collection).

Then, when the async operation completes (in the background), it will add an event to the event queue to call it's callback. If nothing else is currently running in node at that time, then that async callback event will run. If something else is currently running in node, then that operation will finish executing and when it does, the next event in the event queue will run and so on...

In this way, there is only ever one thread of execution at a time in nodejs. When one finishes, the nodejs execution engine gets the next event off the event queue and runs it. When async operations finish and want to call their callback, they insert an event into the event queue.

This answer which describes the event queue and contains some other references was written for a browser, but pretty much all the same rules apply in nodejs so it might also help you understand more about this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...