Reputation: 71
I am running the following Python script in Node.js through python-shell:
import sys
import time
x=0
completeData = "";
while x<800:
crgb = ""+x;
print crgb
completeData = completeData + crgb + "@";
time.sleep(.0001)
x = x+1
file = open("sensorData.txt", "w")
file.write(completeData)
file.close()
sys.stdout.flush()
else:
print "Device not found\n"
And my corresponding Node.js code is:
var PythonShell = require('python-shell');
PythonShell.run('sensor.py', function (err) {
if (err) throw err;
console.log('finished');
});
console.log ("Now reading data");
Output is:
Now reading data
finished
But expected output is:
finished
Now reading data
Node.js can not execute my Python script synchronously, it executes first all the code following the PythonShell.run
function then executes PythonShell.run
. How can I execute first PythonShell.run
then the following code? Any help will be mostly appreciated... It is an emergency please!
Upvotes: 6
Views: 5486
Reputation: 39
It work for me:
let {PythonShell} = require('python-shell');
var options = {
mode: 'text',
pythonPath: 'python',
pythonOptions: [],
scriptPath: '',
args: []
};
async function runTest()
{
const { success, err = '', results } = await new Promise(
(resolve, reject) =>
{
PythonShell.run('hello.py', options,
function (err, results)
{
if (err)
{
reject({ success: false, err });
}
console.log('PythonShell results: %j', results);
resolve({ success: true, results });
}
);
}
);
console.log("python call ends");
if (! success)
{
console.log("Test Error: " + err);
return;
}
console.log("The result is: " + results);
// My code here
console.log("end runTest()");
}
console.log('start ...');
runTest();
console.log('... end main');
The result is:
start ...
... end main
PythonShell results: ["Hello World!"]
python call ends
The result is: Hello World!
end runTest()
Upvotes: 3
Reputation: 11
I had the same problem and resolved it by making a promise with the PythonShell.run
method inside it. Then, you only have to await that promise and your code runs synchronously.
The promisify method:
RunPythonScript: function(scriptPath, args, pythonFile){
let options = {
mode: 'text',
pythonPath: 'python',
pythonOptions: [],
scriptPath: scriptPath,
args: args,
};
return new Promise((resolve,reject) =>{
try{
PythonShell.run(pythonFile, options, function(err, results) {
if (err) {console.log(err);}
// results is an array consisting of messages collected during execution
console.log('results', results);
resolve();
});
}
catch{
console.log('error running python code')
reject();
}
})
},
Then, you await the promise:
await RunPythonScript(YOURscriptPath,YOURargsScript,YOURpythonFile);
Upvotes: 1
Reputation: 1
export default {
data () {
return {
}
},
method: {
callfunction () {
console.log ("Now reading data");
},
pyth (){
var PythonShell = require('python-shell');
var vm = this; //save the object (this) in variable vm to use inside pythonshell.run
PythonShell.run('sensor.py', function (err) {
if (err) throw err;
console.log('finished');
vm.callfunction();
});
}
}
}
Upvotes: -1
Reputation: 140256
As this is asynchronous, add an end callback (found in the documentation) instead of instructions at top level
// end the input stream and allow the process to exit
pyshell.end(function (err) {
if (err) throw err;
console.log ("Now reading data");
});
Upvotes: 0