Wednesday 27 February 2019

I want to spawn processes in a for loop, but I want the for loop to wait until the previous process finished to start the next one.

I am currently trying to understand child_process.spawn, I have an array of urls to download with aria2c, so my current code looks like this:for (i in fileLinks) { let arguments = '-x 15 -s 15 "' + fileLinks[i] + '" --file-allocation trunc --max-tries 100 --retry-wait 10'; let child = spawn("aria2c", arguments.split(" ")); child.stdout.on('data', (data) => { console.log(`stdout: ${data}`); }); child.stderr.on('data', (data) => { console.log(`stderr: ${data}`); }); child.on('close', (code) => { if (code != 0) { fs.writeFile("error.log", "url=" + fileLinks[i]\n"); } }); //I can't figure out how to block for loop without blocking stdout showing on the terminal } I want to have only 1 aria2c process running at one time, I know there is spawnSync in Node.js, but I won't be able to show stdout in real time if I use that.I also know aria2c can read url list from text file, but I can't figure out how to make Node.js understand which file is fully downloaded or not.

Submitted February 27, 2019 at 06:38AM by axzxc1236

No comments:

Post a Comment