forked from KanjiVG/kanjivg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.js
48 lines (39 loc) · 1.43 KB
/
convert.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const { Worker } = require('worker_threads');
const os = require('os');
const fs = require('fs');
const path = require('path');
const targetDirectory = './kanjiSVG';
const numCPUs = os.cpus().length;
fs.readdir(targetDirectory, (err, files) => {
if (err) {
console.error(`Error reading directory: ${err}`);
return;
}
const svgFiles = files.filter(file => path.extname(file) === '.svg');
const totalFiles = svgFiles.length;
const filesPerThread = Math.ceil(totalFiles / numCPUs);
let completedThreads = 0;
for (let i = 0; i < numCPUs; i++) {
const worker = new Worker('./convertWorker.js');
const startIndex = filesPerThread * i;
const endIndex = Math.min(startIndex + filesPerThread, totalFiles);
const fileSubset = svgFiles.slice(startIndex, endIndex);
worker.postMessage({ targetDirectory, fileSubset });
worker.on('message', message => {
console.log(message);
});
worker.on('error', error => {
console.error('Worker error:', error);
});
worker.on('exit', code => {
if (code !== 0) {
console.error(`Worker stopped with exit code ${code}`);
} else {
completedThreads++;
if (completedThreads === numCPUs) {
console.log('All workers completed successfully.');
}
}
});
}
});