This is following my earlier question Node.js Non Blocking Nature . I still can't feel I completely understand what is happening with Node.js . Lets say Node got a request for a big file upload from a user . Now we say we put the code to handle with the file in a callback , thus making it asynchronous . What happens next ? Lets say the callback got put in a big list of events the Node.js app needs to take care of , so thus far it is NOT blocking . But at some point the callback gets pulled out from the event loop and it's code starts running (uploading or downloading the file , whatever) . What now ? Is the Node.js app being blocked now ? If so - how does it not destroy the app's performance , and if not - who is in charge of 'jumping' between different events and requests ? We understand that in apache or similar servers there is special code to handle all the threads(timing them) so that no thread chokes all the others .In Node however , as I understand , there is an event loop . When the file I\O finishes , it's callback magically returns with the ready file . Nevertheless , the server still has to do the I\O , no escaping that ! Is the code in the event loop being timed ? For example jumping between different events(callbacks..whatever) so that no request chokes the other? If so , in what manner ?
All the I/O is handled internally by node.js using multiple threads; it's the programming interface to that I/O functionality that's single threaded, event-based, and asynchronous.
So the big upload of your example is performed by a separate thread that's managed by node.js, and when that thread completes its work, your callback is put onto the event queue (to be executed by the single JavaScript thread).
Think of the single JavaScript thread as pulling callbacks off the head of the queue and executing them serially until the queue is empty. Then it sleeps until one of the internal I/O threads puts a new callback on the queue.
This article discusses the related topic of process.nextTick which helped me understand some of these details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With