Search code examples
javascriptnode.jsparallel-processingchild-processspawn

How to spawn five child processes in parallel


I want to run five spawn commands in parallel. I pass five hls stream urls to the loop, and these streamlink commands should record the video for 5 seconds and then kill those processes.

I tried to do it async in several ways... but I don't know how to wait those 5 seconds independently for each stream.

I'm running this on windows 10.

Here's the last thing I tried:

import { spawn } from "child_process";
import crypto from "crypto"

const hls_streams = [
    'https://stream1.url',
    'https://stream2.url',
    'https://stream3.url',
    'https://stream4.url',
    'https://stream5.url',
]

for (let i = 0; i < hls_streams.length; i++) {
    const filename = crypto.randomBytes(16).toString("hex");
    const child = spawn('streamlink', [`${urls[i]}`, "best", "-f", "-o", `/temp/${filename}.mp4`]);
    await new Promise(r => setTimeout(r, 5000));
    child.kill()
}

A correct execution should last only 5 seconds for the five urls...


Solution

  • You could use a loop for creating an array of children, then wait, then another loop for ending them. For creating an array based on an already existing one, map() may be more convenient:

    import { spawn } from "child_process";
    import crypto from "crypto";
    
    const hls_streams = [
        'https://stream1.url',
        'https://stream2.url',
        'https://stream3.url',
        'https://stream4.url',
        'https://stream5.url',
    ];
    
    let children = hls_streams.map(url => {
        const filename = crypto.randomBytes(16).toString("hex");
        const child = spawn('streamlink', [`${url}`, "best", "-f", "-o", `/temp/${filename}.mp4`]);
        return child;
    });
    
    await new Promise(r => setTimeout(r, 5000));
    
    for(let child of children) {
        child.kill();
    }