作者:三八xuan_624 | 来源:互联网 | 2023-05-18 08:47
ImtryingtocreateaFIFOpipebetweenapythonfileandCfile,buttheissueisthatwhenreadin
I'm trying to create a FIFO pipe between a python file and C file, but the issue is that when reading in the input from the C file, getline blocks until the writer end (in the python file) closes.
我正在尝试在python文件和C文件之间创建一个FIFO管道,但问题是当读取C文件的输入时,getline会阻塞,直到编写器结束(在python文件中)关闭。
C file:
char fifo_emg[] = "emg";
mkfifo(fifo_emg, S_IRWXU);
int fd_emg = open(fifo_emg, O_RDONLY);
FILE* fp = fdopen(fd_emg, "r");
char *line = NULL;
size_t len = 0;
ssize_t _read;
printf("Both ends open. Reading commands...\n");
while ((_read = getline(&line, &len, fp)) != -1) {
printf("Comamnd: %s", line);
}
Python file:
fifo = open("emg", "w");
while 1:
line = raw_input("ENTER COMMAND: ")
if line[0] == '!':
break
else:
fifo.write(line + '\n')
fifo.close()
When i run both, I want the output from the C file to go "Command: foo" as soon as it is entered through the python input. However, data is only read in when fifo.close() is called, and it is just read in all at once. This is not really helpful since I want a constant stream of commands.
当我同时运行时,我希望C文件的输出在通过python输入输入后立即进入“Command:foo”。但是,只有在调用fifo.close()时才读入数据,并且只是一次性读取数据。这不是很有用,因为我想要一个恒定的命令流。
3 个解决方案