slow non-blocking reads

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark Dufour

    #1

    slow non-blocking reads

    hello all,

    I am trying to fire up a child process using os.popen2, and have the
    parent and child communicate in a non-blocking fashion. it works, but
    somehow it's unbearably slow. the following simulates a blocking
    readline:

    import os, fcntl

    fi, fo = os.popen2('./child')
    fcntl.fcntl(fo. fileno(), fcntl.F_SETFL, os.O_NONBLOCK)

    def getline():
    line = ''
    while 1:
    try:
    line += os.read(fo.file no(), 1)
    if line.endswith(' \n'):
    return line
    except OSError:
    pass

    print getline()

    while 1:
    fi.write('echo echo echo\n')
    fi.flush()
    print getline()

    and this is the child process, written in C:

    tcgetattr(0, &tty);
    tty.c_lflag &= ~ICANON;
    tty.c_cc[VTIME] = 0;
    tty.c_cc[VMIN] = 0;
    tcsetattr(0, TCSADRAIN, &tty);

    fds[0].fd = 0;
    fds[0].events = POLLIN;

    fcntl(0, F_SETFL, O_NONBLOCK);

    printf("start\n ");
    fflush(stdout);

    while(1) {
    if( poll(fds, 1, 0) > 0) {
    if((fds[0].fd == 0) && (fds[0].revents & POLLIN)) {
    read(0, &c, 1);

    printf("%c", c);
    fflush(stdout);
    }
    }
    }

    the child sends a 'start' message and then echoes the parent.

    any thoughts about why this runs extremely slowly?


    thanks!
    mark dufour.
    --
    if vars: self.output('; '.join([self.type(var)+ ' '+name for (name,var)
    in vars.items()])+';')
  • Lawrence D'Oliveiro

    #2
    Re: slow non-blocking reads

    In article <mailman.7620.1 151598431.27775 .python-list@python.org >,
    "Mark Dufour" <mark.dufour@gm ail.comwrote:
    >any thoughts about why this runs extremely slowly?
    Because both processes are chewing up most of your CPU each waiting for
    the other to do something.

    Comment

    Working...