I am trying to execute this piece of node js code which execute a python script. Through this code works fine. but the response "running" and "finished" are displayed on front end immediately. "finshed" has to be displayed once the execution of python scripts gets completed.
app.post('/execute', function(request, response){
response.write("running");
console.log("executing")
var pyshell = new PythonShell('./python_codes/test.py')
pyshell.on('message', function (message) {console.log(message);});
pyshell.end(function (err) {if (err){throw err;};console.log('finished');});
response.write("finished");
response.end();
});
PythonShelluseschild_process.spawn, which is asynchronous - you're saying here "Run this python thing, and tell me when it's done by using the callback I've given you with.end, and while you're doing that I will carry on with my own stuff". I suspect you'll need to either wait forpyshell.terminatedto show the right value, or pass the response into the.endcallbackapp.postcallback is done runningresponse.end()will be called implicitly.