在 python 和 java 之间发送短信

Sending short messages between python and java

我的 java 程序调用一个 python 脚本,该脚本向它发送 JSON 小字符串。我使用进程构建器启动 python 脚本,然后将 inputstream 包装在 inputstreamreaderBufferedReader 中。

不过我注意到,程序会一直等到一定数量的消息已发送后才将数据传输到我的 java 应用程序。

我将这个问题追溯到我从进程中获得的原始 InputStream,因为即使我在没有 BufferedReader 的情况下单独使用它,我仍然会遇到这个问题。当我增加消息的大小时,它们可以毫无问题地传输。但我只需要每隔几秒钟发送一次短信。这种行为让我认为输入流有一些内部缓冲区,它在传输数据之前等待填充。 有什么帮助吗?

您还没有真正提供足够的详细信息以确切了解发生了什么(MCVE 会很好 ;)),但是:

  • 你显然有问题,
  • 听起来可能确实与"buffering"有关。

因此我的建议是 "flush()"。

问:如果您 "flush" 缓冲区 - 谁需要做? reader,还是作者?

A:当然是作家:)

听起来你的 "writer" 是 Python。听起来 Python 应用程序正在写入标准输出。

这可能有帮助:

How often does python flush to a file?

For file operations, Python uses the operating system's default buffering unless you configure it do otherwise. You can specify a buffer size, unbuffered, or line buffered.

For example, the open function takes a buffer size argument.

http://docs.python.org/library/functions.html#open

"The optional buffering argument specifies the file’s desired buffer size:"

0 means unbuffered,
1 means line buffered,
any other positive value means use a buffer of (approximately) that size.

A negative buffering means to use the system default, which is usually 
line buffered for tty devices and fully buffered for other files.

If omitted, the system default is used.