V
V
Vitaly Vitaly2015-04-18 18:18:49
Node.js
Vitaly Vitaly, 2015-04-18 18:18:49

Multithreading solution for node.js?

Hello. There is a need to work in N number of simultaneous threads, which are independent of each other. The structure is this: each thread makes a request and writes to the database, and this happens in a cycle until the thread runs out of cycle.
Only the creation of a number of workers through cluster N comes to mind, but there will be many more workers than the core of the processor.
Maybe there are some other solutions (example) or some suggestions according to my idea?
If possible, please provide an example.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Timur Shemsedinov, 2015-04-18
@V_Tjuryakin

It is possible to make and in 1 flow through queues and asynchrony. We have N arrays A1.. AN, we start for them in a loop for each of our eachSeries, something like this:

async.eachSeries(A1, function(item, cb) {
  // сюда приходит item, когда с ним покончено, то делаем cb()
}, function() {
  // все готово
});

For each array, until the previous element is processed (returns control via callback), the next one will not be processed. If you want to use many processors, then divide the task into parts and start many processes using cluster , passing each part of the work through environment variables or IPC: worker.send .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question