[Typescript] Simple code snippet for MultiProcessing using Pool.

Language/Typescript 2018.06.26 06:30

Very simple code.


Max number of subprocess is number of CPU-cores.


import * as child_process from 'child_process'; import * as os from 'os'; const MAX_POOL = os.cpus().length; export class Task { bin: string; args: string[]; onComplete?: (task: Task, stdout: string, stderr: string) => Promise<void>; onDone?: (task: Task, stdout: string, stderr: string) => Promise<void>; onError?: (task: Task, exitcode: number, stdout: string, stderr: string) => Promise<void>; } export function runInParallel(tasks: Task[]): Promise<number> { let failCnt = 0; let activeCnt = 0; const runNext = function (res: any, rej: any) { const c = tasks.shift(); if (undefined === c) { if (0 >= activeCnt) { res(failCnt); } return; } activeCnt++; child_process.execFile(c.bin, c.args, async (err, stdout, stderr) => { try {

if (err) { failCnt++; if (c.onError) { await c.onError(c, (<any>err)['code'], stdout, stderr); } } else { if (c.onDone) { await c.onDone(c, stdout, stderr); } } if (c.onComplete) { await c.onComplete(c, stdout, stderr); }

} finally {

activeCnt--;

runNext(res, rej);

} }); }; return new Promise((res, rej) => { while (activeCnt < MAX_POOL && tasks.length > 0) { runNext(res, rej); } }); }

Buffered stdout and stderr is used.


Trackback 0 : Comment 0