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

2021-04-25 11:16:05 +08:00
 yinggcy

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

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

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

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

1418 次点击
所在节点    Node.js
3 条回复
formulahendry
2021-04-25 11:23:55 +08:00
试试 -u:`python -u ***.py `
yinggcy
2021-04-25 11:34:50 +08:00
@formulahendry 谢谢! 我一直以为是 node 的问题, 查了好久, 没想到是 python 的问题
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'

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/773072

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX