async icon indicating copy to clipboard operation
async copied to clipboard

Multicore

Open jfoclpf opened this issue 3 years ago • 1 comments

I tried to search in closed topics but found nothing. Does this lib support multicore?

Imagine I have an array with 1 million filenames: ['1.txt', '2.txt', etc., ..., '1000000.txt'] and I need to do heavy processing and then write the result in the respective file?

What would be the method to efficiently use all the cores of the CPU to spread the CPU intensive processing towards different cores among different filenames?

Normally I would use this:

const fs = require('fs')
const fs = require('async')
const heavyProcessing = require('./heavyProcessing.js')

const files = ['1.txt', '2.txt', ..., '1000000.txt']

async.each(files, function (file, cb) {
  fs.writeFile(file, heavyProcessing(file), function (err) {
    if (err) cb(Error(err)) else cb()
  })
})

I guess the heavyProcessing function is always run within the same thread. Am I right? Can I spawn it towards different cores/threads to improve overall speed?

jfoclpf avatar Sep 01 '22 18:09 jfoclpf

Just for everyone to know, if they are interested, you need to use the npm module piscina.

In this gist I explain everything.

jfoclpf avatar Sep 02 '22 15:09 jfoclpf

This library is about managing the cooperative multi-tasking within a single JS thread. It doesn't handle multi-threading or multi-core.

aearly avatar Oct 01 '22 22:10 aearly