我通过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"
Run Code Online (Sandbox Code Playgroud)
我对应的 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");
Run Code Online (Sandbox Code Playgroud)
输出是:
Now reading data
finished
Run Code Online (Sandbox Code Playgroud)
但预期输出是:
finished
Now reading data
Run Code Online (Sandbox Code Playgroud)
Node.js 无法同步执行我的 …