从命名管道读取连续数据 [英] Reading continuous data from a named pipe

查看:535
本文介绍了从命名管道读取连续数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图从命名管道读取连续数据.但是由于某种原因,如果我不延迟,接收器将停止读取,并且在进行一些采样后仅显示空白屏幕.

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.

我需要发送可能以毫秒为单位的连续数据,所以这就是为什么延迟不起作用的原因.我试图首先使用while循环对其进行仿真(真正的脚本将读取财务数据).这是我的第一次尝试:

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:

这是发件人,一个python脚本:

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

这是C/C ++中的接收器:

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?

推荐答案

首先,您不需要为每个I/O操作打开/关闭管道. 最终您可能需要在每次写入后刷新输出.

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.

然后,当您输出基于行的文本数据时,您实际上不能真正依靠固定宽度的读取来取回数据.以您的示例为例,我只需要读入一个字符串即可,并且istream应该读到下一个空格(此处为\n\r)

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 >>被重载以从istream读取字符串.在这种情况下,它将从流中提取字符,并在遇到空白字符或遇到流末尾时立即停止.广义上讲,这允许逐字读取输入.

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

这篇关于从命名管道读取连续数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆