强制 netcat 立即发送消息(无缓冲) [英] Force netcat to send messages immediately (without buffering)

查看:65
本文介绍了强制 netcat 立即发送消息(无缓冲)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python脚本通过标准print()参数生成数据.然后,脚本的输出应发送到localhost tcp socket 9999.

I'm using a python script to generate data using standard print() arguments. The output from the script should then be sent to localhost tcp socket 9999.

python script.py | nc -lk 9999

在客户端,我正在侦听localhost tcp套接字9999,以验证一切正常.

On the client side I'm listening to localhost tcp socket 9999 to verify everything is working fine.

nc localhost 9999

是的,它正在工作.但是看起来nc在发送消息之前正在缓冲一些消息.结果,我在客户端遇到了巨大的延迟,这对于我的应用程序是不可接受的:我需要尽快获得数据.

And yes, it's working. But it looks like nc is buffering some messages before sending them. As a result, I get huge delays on the client side, which is not acceptable for my application: I need the data asap.

是否可以禁用缓冲区?

我注意到,如果我发送更多消息,则延迟会减少.但是,这并不是我真正的解决方案.

I noticed that if I send more messages, the delay decreases. However, this isn't really a solution for me.

推荐答案

您可以使用 stdbuf 命令:

stdbuf -o0 python script.py | stdbuf -i0 nc -lk 9999

或python的-u Unbuffered选项:

or the -u Unbuffered option for python:

python -u script.py | stdbuf -i0  nc -lk 9999

I/O缓冲是由libc处理的,除非程序自己明确地对其进行处理. stdbuf 影响该行为.进一步阅读 man stdbuf .

I/O buffering is, unless a program explicitly handles it on it's own, handled by the libc. stdbuf influences that behaviour. Further reading man stdbuf.

这篇关于强制 netcat 立即发送消息(无缓冲)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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