0

I've been working on a very simple node.Js server with some routes. I tried to implement one route that will be able to run a python(3.6) script every time it is reached, using the Node Js child-process package. The problem is : When I try to do so, running my script with the right command line args, nothing happens.

I already tried to run a simple command like creating an empty file or just a simple pwd everything works fine. secondly, as you can see below, in my python script, i'm using the logging python package to keep trace of the workflow. Unfortunatly, my logfile.log is empty. I also tried to get the stream back from python and print in the standard output : nothing. the path to the script is correct so i don't really know what difference between my manual execution of the script and node.js's.

Node.Js code:

app.get('/trigger',(req, res) => {
        console.log('/trigger');
        console.log(__dirname + "/../scripts/launcher.py")
        var key = req.query['Key'];
        const spawn = require('child_process').spawn;
        const pythonProcess = spawn("python3.6", [__dirname + "/../scripts/launcher.py", key]);
        console.log("process started with pid : " + pythonProcess.pid);
        pythonProcess.stdout.on('data', (data) => {
                console.log("child stdout")
                console.log(`\n${data}`);
        });
        res.status(200).send()
})

Python code :


def init_logger():
    # create logger with 'spam_application'
    logger = logging.getLogger('spam_application')
    logger.setLevel(logging.DEBUG)

    # Load variable to env
    load_dotenv()

    # create file handler which logs even debug messages
    fh = logging.FileHandler(dir_path + '/logfile.log')
    fh.setLevel(logging.DEBUG)

    # create formatter
    formatter = logging.Formatter("%(asctime)s; %(levelname)s ; %(message)s",
                                  "%d-%m-%Y %H:%M:%S ")
    fh.setFormatter(formatter)  # add formatter to ch
    logger.addHandler(fh)  # add ch to logger
    return logger


def main():
    logger = init_logger()
    logger.info("logfile has been updated :D")

    # Downloading mail before processing it
    raw_mail = fetch_mail(sys.argv[1])
    mailer = MailManager(logger)
    if mailer.entry_point(raw_mail) == -1:
        logger.error("Sender Not Authorized")
        return -1
    uploader = Uploader(logger)
    uploader.initialize_warning_count()
    result = mailer.get_mailobj()
    uploader.entry_point(result)
    return 0

and finally Node.js pm2 logs :

0|| 2019-09-23T12:29:55: /trigger
0|| 2019-09-23T12:29:55: my/path/to/script/launcher.py
0|| 2019-09-23T12:29:55: process started with pid : 19068

So I have a PID, which means the command is running well (i'm not sure). I would like to be able to have the python's log inside logfile.log (as when I was testing manually). With that log I will be able to know what's realyy going on.

Thanks guys, if you need more information about my issue please comment or be free to edit.

Environment : python3.6, node v8.10.0 in an Ubuntu EC2 instance.

1 Answer 1

1

When you spawn a process with node, if you want to get the stdio output you need to pass it as an option, so do something like:

const pythonProcess = spawn("python3.6", [__dirname + "/../scripts/launcher.py", key], { stdio: 'inherit' });

Check: https://nodejs.org/api/child_process.html#child_process_options_stdio

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

1 Comment

marked as answer, it allows me to see direct exception raising by Python. Btw, the my problem was coming ffrom the fact that manual and node js environment variable was different. PYTHONPATH wasn't existing in node, so the python import in my launcher.py wasn't working. it also stop preventing python from updating the logfile.log ! thanks !

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.