Reputation: 1634
I have been trying to read continuous data from a named pipe. But for some reason if I don't put a delay, the receiver will just stop reading and only a blank screen is shown after a few samples.
I need to send continuous data that might change in milliseconds, so that's why putting a delay wouldn't work. I am trying to simulate it first using a while loop (the real script will be reading financial data). This is my first attempt:
This is the sender, a python script:
import os
import time
try:
os.remove("/tmp/pipe7") # delete
except:
print "Pipe already exists"
os.mkfifo("/tmp/pipe7") # Create pipe
x = 0
while True:
x = time.time()
pipe = open("/tmp/pipe7", "w")
line = str(x) + "\r\n\0"
pipe.write(line)
pipe.close()
#time.sleep(1)
os.remove("/tmp/pipe7") # delete
This is the receiver in C/C++:
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <iostream>
#include <sys/stat.h>
#define MAX_BUF 1024
using namespace std;
int main()
{
while(1){
char buf[MAX_BUF];
memset (buf, 0, sizeof(buf)); //Clearing the message buffer
int fd = open("/tmp/pipe7", O_RDONLY); // Open the pipe
read(fd, buf, MAX_BUF); // Read the message - unblock the writing process
cout << buf << endl;
close(fd); // Close the pipe
}
return 0;
}
What's wrong with my approach? And what's the best way to communicate continuously between the two programs using pipes?
Upvotes: 2
Views: 3337
Reputation: 51980
First, you don't need to open/close the pipe for each I/O operation. Eventually you might need to flush the output after each writing though.
Then, as you output line-based text data, you cannot really rely on a fixed width reading to get back your data. Given your example, I would simply read into a string -- and the istream
should read up to the next blank (here \n\r
)
All of this lead to something like that (untested -- beware of typos!):
with open("/tmp/pipe7", "w") as pipe:
while True:
x = time.time()
line = str(x) + "\r\n"
pipe.write(line)
pipe.flush()
# in real code, should somehow break the loop at some point
std::ifstream pipe("/tmp/pipe7"); // Open the pipe
while(1){
std::string istr;
pipe >> istr;
cout << istr << endl;
# In real code, should somehow break the loop at some point
}
close(fd);
operator >>
is overloaded to read string from an istream. In that case, it will extracts characters from the stream and stop as soon as either a whitespace character is encountered or the end-of-stream is encountered. Broadly speaking, this allow to read back the input "word by word".
Upvotes: 2