I can't say I have experience implementing multiprocessing in Electron, but perhaps the solution I use in my Node.js projects might suit your needs — it supports dynamic imports.
Let me provide a simple example in TypeScript, which can easily be converted to JavaScript if needed.
npm i multiprocessor
// File: src/index.ts
import { Pool } from 'multiprocessor';
const poolSize = 4;
const pool = new Pool(poolSize);
const input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const result = await pool.map(input, calcSinTask);
pool.close();
console.log(result);
// [ 0.8414, 0.9092, 0.1411, ... ]
async function calcSinTask(x: number): Promise<number> {
const dirName = __dirname.replace('/node_modules/multiprocessor/lib', '/src');
const { calcSin } = await import(`${dirName}/path/to/your/module`);
return calcSin(x);
}
// File: src/path/to/your/module.ts
export function calcSin(x: number): number {
let result = 0;
let sign = 1;
let power = x;
let factorial = 1;
for (let n = 0; n < 1000000; n++) {
if (n > 0) {
factorial *= (2 * n) * (2 * n + 1);
power *= x * x;
sign *= -1;
}
const delta = calcDelta(sign, power, factorial);
if (isNaN(result + delta)) {
return result
}
result += delta;
}
return result;
}
function calcDelta(sign: number, power: number, factorial: number): number {
return sign * (power / factorial);
}
You can clone and run this solution from my example repository.