V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
yinggcy
V2EX  ›  Node.js

node.js spawn stdout.on 不会立即传回数据

  •  1
     
  •   yinggcy · 2021-04-25 11:16:05 +08:00 · 1404 次点击
    这是一个创建于 1090 天前的主题,其中的信息可能已经有所发展或是发生改变。

    a = 1 while True: print(a) a += 1 time.sleep(0.01)

    spawn 运行这个 python 程序, stdout.on 几分钟响应一次, 并且一次传回几千个数据.

    几分钟一次太长了, 至少也要几秒钟一次吧

    如何让数据输出立即传回? 谢谢!

    3 条回复    2021-04-25 15:31:59 +08:00
    formulahendry
        1
    formulahendry  
       2021-04-25 11:23:55 +08:00
    试试 -u:`python -u ***.py `
    yinggcy
        2
    yinggcy  
    OP
       2021-04-25 11:34:50 +08:00
    @formulahendry 谢谢! 我一直以为是 node 的问题, 查了好久, 没想到是 python 的问题
    ZhaoHuiLiu
        3
    ZhaoHuiLiu  
       2021-04-25 15:31:59 +08:00
    我帮你查到有用的资料了

    python 关闭 stdout 输出缓冲 python -u xxx.py

    class Unbuffered(object):
    def __init__(self, stream):
    self.stream = stream
    def write(self, data):
    self.stream.write(data)
    self.stream.flush()
    def __getattr__(self, attr):
    return getattr(self.stream, attr)
    import sys
    sys.stdout = Unbuffered(sys.stdout)
    print 'Hello'
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3460 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 04:52 · PVG 12:52 · LAX 21:52 · JFK 00:52
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.