我通过python-shell在Node.js中运行以下Python脚本:
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"
而我的相应的Node.js代码是:
var PythonShell = require('python-shell');
PythonShell.run('sensor.py', function (err) {
if (err) throw err;
console.log('finished');
});
console.log ("Now reading data");
输出结果为:
Now reading data
finished
但是预期输出结果应该是:
finished
Now reading data
Node.js无法同步执行我的Python脚本,它会先执行PythonShell.run
函数后面的所有代码,然后再执行PythonShell.run
。我该如何先执行PythonShell.run
,再执行后续代码?非常感谢您的帮助...这是一个紧急情况!