https://github.com/smoren/multiprocessor-example-ts
Typescript Multiprocessor Example With Import
https://github.com/smoren/multiprocessor-example-ts
dynamic-import dynamic-imports import javascript multiprocess multiprocessing typescript
Last synced: 7 months ago
JSON representation
Typescript Multiprocessor Example With Import
- Host: GitHub
- URL: https://github.com/smoren/multiprocessor-example-ts
- Owner: Smoren
- License: mit
- Created: 2025-01-19T22:03:39.000Z (9 months ago)
- Default Branch: master
- Last Pushed: 2025-01-21T07:42:41.000Z (9 months ago)
- Last Synced: 2025-01-21T08:27:34.495Z (9 months ago)
- Topics: dynamic-import, dynamic-imports, import, javascript, multiprocess, multiprocessing, typescript
- Language: TypeScript
- Homepage:
- Size: 1.95 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Typescript Multiprocessor Example With Import
## What is multiprocessor?
[Multiprocessor](https://github.com/Smoren/multiprocessor-ts) is a NodeJS/Typescript library that allows you to run
tasks in parallel using child processes.## How it works
```typescript
// 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, {
onTaskSuccess: (result: number, input: number, index: number) => {
console.log(`Task #${index} | result: ${result}, input: ${input}`);
},
onTaskError: (error: string, input: number, index: number) => {
console.log(`Task #${index} | error: ${error}, input: ${input}`);
}
});
pool.close();console.log(result);
// [ 0.8414, 0.9092, 0.1411, ... ]async function calcSinTask(x: number): Promise {
const dirName = __dirname.replace('/node_modules/multiprocessor/lib', '/src');
const { calcSin } = await import(`${dirName}/path/to/your/module`);
return calcSin(x);
}
``````typescript
// 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);
}
```## How to run
```bash
git clone https://github.com/Smoren/multiprocessor-example-ts.git
cd multiprocessor-example-ts
npm i
npm run main
```## License
Multiprocessor TS is licensed under the MIT License.