简体   繁体   中英

Reading continuous data from a named pipe

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?

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".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM