slow non-blocking reads


M

Mark Dufour

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.fileno(), 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.
 
Ad

Advertisements

L

Lawrence D'Oliveiro

"Mark Dufour said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top