Node.js的python子脚本完成输出,而不是实时输出 [英] Node.js's python child script outputting on finish, not real time

查看:119
本文介绍了Node.js的python子脚本完成输出,而不是实时输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是node.js和socket.io的新手,我试图编写一个小型服务器,该服务器将根据python输出更新网页。

I am new to node.js and socket.io and I am trying to write a small server that will update a webpage based on python output.

最终这将用于温度传感器,所以现在我有了一个虚拟脚本,该脚本每隔几秒钟打印一次温度值:

Eventually this will be used for a temperature sensor so for now I have a dummy script which prints temperature values every few seconds:

Thermostat.py

import random, time
for x in range(10):
    print(str(random.randint(23,28))+" C")
    time.sleep(random.uniform(0.4,5))

这是服务器的简化版本:

Here's a cut down version of the server:

Index.js

var sys   = require('sys'), 
    spawn = require('child_process').spawn, 
    thermostat = spawn('python', ["thermostat.py"]),
    app = require('express')(),
    http = require('http').Server(app),
    io = require('socket.io')(http);

thermostat.stdout.on('data', function (output) { 
    var temp = String(output);
    console.log(temp);
    io.sockets.emit('temp-update', { data: temp});
}); 

app.get('/', function(req, res){
    res.sendFile(__dirname + '/index.html');
    });

最后是网页:

Index.html

<!doctype html>
<html>
    <head>
        <title>Live temperature</title>
        <link rel="stylesheet" type="text/css" href="styles.css">
    </head>
    <body>
    <div id="liveTemp">Loading...</div>

    <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
    <script src="/socket.io/socket.io.js"></script>
    <script>
        var socket = io();
        socket.on('temp-update', function (msg) {
        $('#liveTemp').html(msg.data)
    });
    </script>

    </body>
</html>

问题是nodejs似乎一次接收所有温度值,而不是获得10个温度值以随机的时间间隔,在脚本完成后,我会在一个长字符串中得到所有的 all 值:

The problem is nodejs seems to recieve all of the temperature values at once, and instead of getting 10 temperature values at random intervals, I get all of the values in one long string after the script has finished:


推荐答案

您需要在python中禁用输出缓冲。这可以通过许多不同的方式完成,包括:

You need to disable output buffering in python. This can be done many different ways, including:


  • 设置 PYTHONUNBUFFERED 环境变量

  • -u 开关传递给python可执行文件

  • 调用 sys.stdout.flush()(或您的情况下为 print()

  • 对于Python 3.3+,您可以将 flush = true 传递给 print() print('Hello World!',flush = True)

  • Setting the PYTHONUNBUFFERED environment variable
  • Passing the -u switch to the python executable
  • Calling sys.stdout.flush() after each write (or print() in your case) to stdout
  • For Python 3.3+ you can pass flush=true to print(): print('Hello World!', flush=True)

另外,在您的节点代码中,(即使您在python代码中已经入睡并且现在正在刷新标准输出),您实际上也不应该假定的'数据'处理程序中的 output thermostat.stdout 总是只有一行。

Additionally, in your node code, (even though you have a sleep in your python code and you are now flushing stdout) you really should not assume that output in your 'data' handler for thermostat.stdout is always going to be just one line.

这篇关于Node.js的python子脚本完成输出,而不是实时输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆