0

I have this download manager that will donwload files from queue[] in a wget separated child process each.

However, after the download is complete and the callback is called I need to handle queue[i] but I don't know which queue index is it.

const EXEC_FILE = require('child_process').execFile; /** https://nodejs.org/api/child_process.html */

queue = []; /** array with download urls */

function startDownload(i){    
    var downloadProccess = EXEC_FILE(
        wget, [ queue[i] ],
        (error, stdout, stderr) => { 

            /** callback after EXEC_FILE completed or aborted */
            /** HERE! How do I get the index i? */

    });   
}

startDownload(1);
startDownload(3);
startDownload(9);

How do I get the i value after the process is complete?

Azevedo
  • 2,059
  • 6
  • 34
  • 52
  • WARNING: Node.js multithreading is not meant to be used like this. For your task asynchronous downloading would be more efficient. Try 'request' module for downloading in a foreach loop. To be more robust you can use request-promise along with bluebird module. This link will be of help, try it in a loop: http://stackoverflow.com/a/11944984/3359432 – Nidhin David Nov 05 '16 at 15:06
  • 1
    @NidhinDavid I can't seem to find anything wrong with this approach: Offloading heavy tasks from the main process. Could you explain a bit about how should it be used if I'm missing anything? – code-jaff Nov 05 '16 at 15:57
  • @code-jaff Multithreading in Node.js is not like other programing languages. Infact when you 'multithread' its spwaning and entire new instance of Node.js process. You can only communicate between them using message passing. And as far as Node.js is concerned IO related tasks is not heavy. Node.js is exactly ment for these kind of tasks. If it was an image processing then I would agree with you, even then the best option is to create a 'addon' in c++ rather than offloading. it gives more control. – Nidhin David Nov 06 '16 at 16:36
  • @NidhinDavid In this example only I used wget. In the real scenario the download app handles more complex things. Aria2c for example, it is not a simple download tool, it can handles torrent protocol, etc. **Even though**, I see no problem running wget like code-jaff wrote. Why would I write a download module to run in another node.js instance when there is already curl, wget? – Azevedo Nov 07 '16 at 12:48
  • @Azevedo The link I mentioned uses only pure basic Node.js code. The module is just for more features. It even includes oAuth 1. And officially Node.js is single threaded event-driven scripting framework built on top of v8 JS engine. What you call thread is actually an entirely new process with its own memory and resources that aren't shared with parent. And regarding modules, Node.js is all about npm modules.Great people has invented great wheels for us and posted it on NPM, so that we don't need to reinvent the wheel. Most of the modules are pure js but here you are coupling with non js code – Nidhin David Nov 07 '16 at 17:37

1 Answers1

1

Javascript functions are closures. So you already have the variable i inside the function body.

lorefnon
  • 12,875
  • 6
  • 61
  • 93