1

I am currently using the python-shell module in a Node based web interface. The issue I am having is mostly syntactical. The code below shows the generation of a python script.

var PythonShell = require('python-shell');
PythonShell.run('my_script.py' function (err) {
    if (err) throw err;
    console.log('finished');
}):

This is just an example script from here. How do I relate this to node's

var procc = require('child_process'.spawn('mongod');
procc.kill('SIGINT');

The documentation states that PythonShell instances have the following properties:

childProcess: the process instance created via child_process.spawn

But how do I acutally use this? There seems to be a lack of examples when it comes to this specific module

1 Answer 1

6

For example -

var python_process;

router.get('/start_python', function(req, res) {
    const {PythonShell} = require("python-shell");

var options = {
    pythonPath:'local python path'
  }
    var pyshell = new PythonShell('general.py');

    pyshell.end(function (err) {
        if (err) {
            console.log(err);
        }
    });
    python_process = pyshell.childProcess;

    res.send('Started.');
});

router.get('/stop_python', function(req, res) {
   python_process.kill('SIGINT');
   res.send('Stopped');
});

Sign up to request clarification or add additional context in comments.

2 Comments

is router referring to the module router?
This is just an example to show you how to use "childProcess". You can use app or any other middleware that can accept requests.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.