Run Arbitrary Code While Waiting For Callback in N

2019-01-27 07:50发布

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?

2条回答
做个烂人
2楼-- · 2019-01-27 08:43

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.

查看更多
Rolldiameter
3楼-- · 2019-01-27 08:53

It's doing nothing while waiting. But it might wait for multiple IO actions to finish, and then will invoke the callback for the one that finishes first. So if you are waiting for multiple connections, node will do something everytime a connection opens. It will even do that if some code triggered by the first connection has registered a callback to wait for some file IO in the meantime.

查看更多
登录 后发表回答