سؤال

In this question, the asker has addressed the problem of reading from a named pipe in a non-blocking manner, but he uses a fixed buffer size. Is there a way to do this without a fixed buffer size and just waiting for the other end to terminate their own buffer with a newline character?

هل كانت مفيدة؟

المحلول

Assuming that your delimiter is a you can read multiple variable length strings in a non-blocking manner, as shown in this program which counts while receiving output from a named pipe.

import os
import time
import errno
import sys

io = os.open(expanduser("~/named_pipes/cob_input"), os.O_RDONLY | os.O_NONBLOCK)

# For implementing non-blocking IO
def read_pipe_non_blocking(input_pipe, size):
    try:
        in_buffer = os.read(input_pipe, size)
    except OSError as err:
        if err.errno == errno.EAGAIN or err.errno == errno.EWOULDBLOCK:
            in_buffer = None
        else:
            raise  # something else has happened -- better reraise
    return in_buffer

def get_azimuth(input_pipe):
    in_buffer = read_pipe_non_blocking(input_pipe, 1)
    print(in_buffer)
    if(in_buffer is None):
        sys.stderr.write("n")
        return ""
    else:
        tmp_buffer = None
        while(tmp_buffer != "a"):
            sys.stderr.write("m")
            time.sleep(0.1)
            tmp_buffer = read_pipe_non_blocking(input_pipe, 1)
            if(tmp_buffer != None and tmp_buffer != "a"):
                in_buffer += tmp_buffer
        read_pipe_non_blocking(input_pipe, 1) #Read in the newline character and the toss it
        sys.stderr.write("\nReturning \{%s\}" %in_buffer)
        return in_buffer


i = 0
while 1:
    print i
    time.sleep(1)
    i += 1
    get_azimuth(io)

This code has been directly copy pasted from my code and isn't really that clear. If anyone needs clarification, leave a comment.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top