小编Ram*_*zan的帖子

如何在 Node.js 中同步运行 Python 脚本?

我通过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 无法同步执行我的 …

python node.js

5
推荐指数
1
解决办法
2967
查看次数

标签 统计

node.js ×1

python ×1