Wednesday, 15 March 2017

Spawn child process from node to run python script returns 500

I'm trying to spawn a child process to run a python script from Node. I have the following request that comes in:

/webcrawler?source=http://www.pygamers.com&method=BFS&nodeCount=3&depth=0&keyword=game

I have verified that my params are coming in correctly. Here is my code set up to handle the request in app.js:

app.get('/webcrawler', function(req, res){
  var python = require('child_process').spawn(
  'python',
  ["WebCrawler/Webcrawler.py"
  , req.query.source
  , req.query.method
  , req.query.nodeCount
  , req.query.depth
  , req.query.keyword]
  );
  var output = "";
  python.stdout.on('data', function(data){ output += data });
  python.on('close', function(code){
    if (code !== 0) {
        return res.send(500, code);
    }
    return res.send(200, output);
  });
});

I am calling my Python script, Webcrawler.py which is in the WebCrawler directory. The WebCrawler directory is in the same directory as app.js.

However, this request is giving me a 500, and I haven't been able to figure out why. It seems like I must be generating the child process incorrectly. I used this answer as a model for doing so.



via 123

No comments:

Post a Comment