【发布时间】:2013-11-14 15:29:29
【问题描述】:
我有一个使用协议缓冲区的 python 应用程序,以及一个使用协议缓冲区的 Java 应用程序。我想要做的只是能够将消息(序列化后的二进制字符串)打印到标准输出。为此,我在 Python 应用程序中执行以下操作:
def printMessage(self, protobuf_msg):
data = protobuf_msg.SerializeToString()
sys.stdout.write(数据)
sys.stdout.flush()
def main():
protobuf_msg = create_message()
controller.printMessage(protobuf_msg)
然后,我想通过管道输出此输出(python pytonApp | java javaApp)并使用 javaApp 获取此数据并对其进行解析。我尝试了两种选择,使用 Protobuf API:
protected ProtobufMsg receiveMsg() throws Exception{
ProtobufMsg 消息 = null;
message = protobuf_msg.parseFrom(System.in);
返回消息;
}
我也尝试通过以下方式对 BufferedInputStream 执行此操作:
protected ProtobufMsg receiveMsg() throws Exception{
ProtobufMsg 消息 = null;
byte[] data = receiveFromStd();
message = protobuf_msg.parseFrom(data);
返回消息;
}
public byte[] receiveFromStd() 抛出异常{
BufferedInputStream 输入 = 新的 BufferedInputStream(System.in);
字节[] out = 新字节[1024];
int i=0; System.out.println("Entering While"); while((out[i] = (byte)input.read())!= -1){ i++;System.out.println("读取一个字节");
}
byte[] data_out = new byte[i]; for(int l=0; l<data_out.length; l++){ data_out[l]=out[l]; } return data_out;}
所以很明显我做错了什么,但我无法意识到我做错了什么, 因为它停留在 input.read()...
编辑: 我决定改变策略,现在我首先得到数据包的大小,然后是数据包,因为我正在使用 input.read(byte []) 函数...... 我使用的脚本如下:
FIFO_FILE=/tmp/named_$$ # unique name ($$ is the PID of the bash process running this script)
mkfifo $FIFO_FILE
export FIFO_FILE # export the env variable
ant run & # start a background process that reads the env variable and reads the fifo
cat > $FIFO_FILE # reads the standard input and writes to the fifo
rm $FIFO_FILE
我称之为:python pythonApp.py | ./script。
【问题讨论】:
-
readLine() 需要行尾字符才能返回或“解除阻止”。所以也许你还需要添加
sys.stdout.write('\n') -
提示:Stackoverflow 编辑器中的
{}按钮将选定的行转换为正确缩进、突出显示的代码。
标签: java python protocol-buffers stdin