Reputation: 25778
I know that only 1 thread can be run in the node.js program.
If this thread is blocked, for example, waiting for input from keyboard, can it be still responding to other event?
Is there a background event loop thread to distribute event handling?
Upvotes: 0
Views: 136
Reputation: 262794
If this thread is blocked can it be still responding to other event?
It cannot immediately respond, but hopefully those events get queued up to be taken care of when you finally finish your block.
Is there a background event loop thread to distribute event handling?
No background thread. Events are also handled by the "main" (the only) thread.
The rule is to never block in node.js. If something takes time, you must handle it via asynchronous callback. If you want to do CPU-heavy operations concurrently, you have to use multiple processes (and receive the result in an asynchronous callback).
The upside of all this is that you don't have to worry about synchronizing multiple threads (because there is just one). This makes programming safer (but you have to get used to callbacks everywhere).
Upvotes: 1