slow non-blocking reads
Mark Dufour
mark.dufour at gmail.com
Mon Jul 3 08:13:40 EDT 2006
interestingly, leaving out the fcntl stuff makes it work much faster.
it seems to block only sometimes now, for just a moment, but on the
whole the performance is acceptable now.
On 6/29/06, Mark Dufour <mark.dufour at gmail.com> wrote:
> 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.
> --
> if vars: self.output('; '.join([self.type(var)+' '+name for (name,var)
> in vars.items()])+';')
>
--
if vars: self.output('; '.join([self.type(var)+' '+name for (name,var)
in vars.items()])+';')
More information about the Python-list
mailing list