C++与Python命名管道通信问题:C++端无法读取响应
命名管道实现Python与C++同步通信的阻塞问题
我的目标是通过命名管道实现Python与C++进程的同步通信,流程如下:
- C++进程通过
PIPE_1发送以\n分隔的消息 - Python进程读取该消息(同样以
\n分隔),并通过PIPE_2发送响应 - C++进程读取响应
- 重复上述步骤
消息包含多维数组,因此计划使用protobuf库序列化消息后写入管道。但目前C++进程在调用std::getline时阻塞,无法从PIPE_2读取数据,直到手动终止Python进程后才能获取到消息。我怀疑是缓冲问题,尝试过调用flush()以及写入\n,但均无效。
我接受其他性能足够的解决方案(需频繁往返传输消息),但希望找出问题所在。代码运行在Intel架构Mac上(最终部署环境为Linux)。
我用Python重写了C++代码的极简版本,可正常实现同步通信,无阻塞问题,往返消息耗时约10^-4秒。
最小复现代码
Python进程代码
import errno import os pipe1 = '/tmp/pipe1' pipe2 = '/tmp/pipe2' try: os.mkfifo(pipe1, mode=0o777) except OSError as oe: if oe.errno != errno.EEXIST: raise try: os.mkfifo(pipe2, mode=0o777) except OSError as oe: if oe.errno != errno.EEXIST: raise # blocks here until writer opens read_pipe read_pipe = open(pipe1, 'r') write_pipe = open(pipe2, 'w') print('opened pipes') while True: r = read_pipe.readline().strip('\n') print(f'received {r}') if not r: print("Sender terminated") break write_pipe.write(f"Response: {r}\n") write_pipe.flush() print("wrote response")
C++进程代码
#include <fstream> #include <iostream> #include <unistd.h> int main() { std::string pipe1 = "/tmp/pipe1"; std::string pipe2 = "/tmp/pipe2"; int writes = 10; std::ofstream write_pipe(pipe1, std::ios::out); std::ifstream read_pipe(pipe2, std::ios::in); std::cout << "Opened pipes" << std::endl; while (writes--) { write_pipe << "Some useful message" << std::endl; printf("sent message\n"); printf("Reading response...\n"); std::string response; std::getline(read_pipe, response); printf("Received response: %s\n", response.c_str()); } write_pipe.close(); read_pipe.close(); return 0; }
可正常运行的Python版C++代码
import time pipe1 = '/tmp/pipe1' pipe2 = '/tmp/pipe2' write_file = open(pipe1, "w") read_file = open(pipe2, 'r') writes = 10 while writes > 0: writes -= 1 start_time = time.time() write_file.write("Test message" + "\n") write_file.flush() print("sent message") response = read_file.readline() end_time = time.time() print(f"response was {response}")
内容的提问来源于stack exchange,提问作者Jayanth
相关产品推荐
相关产品推荐

