You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
thumbsup/src/components/exiftool/parallel.js

24 lines
931 B
JavaScript

const os = require('node:os')
const _ = require('lodash')
const debug = require('debug')('thumbsup:debug')
const es = require('event-stream')
const exiftool = require('./stream.js')
/*
Fans out the list of files to multiple exiftool processes (default = CPU count)
Returns a single stream of javascript objects, parsed from the JSON response
*/
exports.parse = (rootFolder, filePaths, concurrency) => {
// create several buckets of work
const workers = concurrency || os.cpus().length
const buckets = _.chunk(filePaths, Math.ceil(filePaths.length / workers))
debug(`Split files into ${buckets.length} batches for exiftool`)
// create several <exiftool> streams that can work in parallel
const streams = _.range(buckets.length).map(i => {
debug(`Calling exiftool with ${buckets[i].length} files`)
return exiftool.parse(rootFolder, buckets[i])
})
// merge the object streams
return es.merge(streams)
}