Reputation: 67
I have a web server that has a Node process running on it, managed by pm2 and the web server has 16 available cores.
The Node process manages a queue of tasks utilising the async module. This queue of tasks, depending on the number of events coming in, can grow to over 10,000 in the event of a disconnection between the Node process and the service it interacts with.
The queue will maintain this list of tasks in the event of a disconnection, and then when the connection is re-established, the queued tasks are then executed.
The docs say this...
Tasks added to the queue are processed in parallel (up to the concurrency limit). If all workers are in progress, the task is queued until one becomes available. Once a worker completes a task, that task's callback is called.
My questions are these...
If the queue had 10,000 tasks in it, and I set the concurrency level to 1, am I right in thinking that these will essentially be executed 1 at a time? If so, I guess this means that if new tasks are added, this queue could potentially never be fully drained?
If I was to set the concurrency value to 16, would it essentially run 16 tasks in parallel, making use of each CPU core? Or is the concurrency managed by something else?
How are tasks ran in parallel? My understanding is that a singe Node process can only utilise one core at a time because it's single threaded.
Am I totally missing the point as to how the async modules manages parallel tasks?!
Thanks in advance you clever bunch!
Upvotes: 3
Views: 288
Reputation: 1027
1) Yes , they will be executed in series . If you don’t set a timeout for a task then you may never drain the queue .
2) no. All tasks will be executed on the same core your node app runs on . Spread the tasks to workers and you will gain multi process queue(take a look on child process & ipc)
3) if the tasks are a blocking code , nothing will be in parallel . But if you write to disk / network , you’re doing async work , and while waiting for an answer , the engine continues with the rest of the code
4) just a bit . But you’re on the way to nail it . ;)
Upvotes: 2