0

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.

Community
  • 1
  • 1
123
  • 8,733
  • 14
  • 57
  • 99
  • Either try removing Webcrawler as a part of the path you are instructing node, or give it an absolute path would be my guess - but I don't know node all that well. Might also be useful to know what the 500 is about. – James R Mar 15 '17 at 20:26
  • Check if your python script exits with your targeted exit code. Otherwise check the signal which is also passed as a result parameter to the close event. Do some further analysis with both parameters to check your code validity. See https://nodejs.org/api/child_process.html#child_process_event_close – DmiN Mar 15 '17 at 20:40

3 Answers3

0

It needs to be an absolute path, like /home/username/Webcrawler/webcrawler.py

Olivercodes
  • 1,048
  • 5
  • 17
0

Sounds like a path problem. Also checkout python-shell package. Makes your life so much easier.

0

you can check this package on npm- native-python

it provides a very simple and powerful way to run python functions from node might solve your problem. import { runFunction } from '@guydev/native-python'

const example = async () => {
   const input = [1,[1,2,3],{'foo':'bar'}]
   const { error, data } = await runFunction('/path/to/file.py','hello_world', '/path/to/python', input)

   // error will be null if no error occured.
   if (error) {
       console.log('Error: ', error)
   }

   else {
       console.log('Success: ', data)
       // prints data or null if function has no return value
   }
}

python module

# module: file.py

def hello_world(a,b,c):
    print( type(a), a) 
    # <class 'int'>, 1

    print(type(b),b)
    # <class 'list'>, [1,2,3]

    print(type(c),c)
    # <class 'dict'>, {'foo':'bar'}
Guy-dev
  • 21
  • 4
  • It looks like you are promoting your own work. [Please see the instructions on self-promotion](https://stackoverflow.com/help/promotion): "_you **must** disclose your affiliation in your post._" – starball Dec 11 '22 at 20:52