read and readline hanging

Olivier Lefevre lefevrol at
Sun Jan 27 20:08:48 CET 2008

>> Indeed, if I do this interactively, I can tell after 3 lines that I've
>> gotten all there is to get right now and the fourth readline() call
>> hangs.
> Can you really? 

Yes interactively: at the command prompt, you can tell when it's over
because you know the command you just sent and whether it requires an
answer and of which kind. Also, even if there is no answer you get a
fresh prompt when the interpreter is done.

> Unless there is some way to differentiate between the last line
> and all the other lines of a response, you can't really be sure.

Yes, that has since occurred to me. I need to echo some magic string
after each command to know that I reached the end of the answer to
the previous command. In interactive mode the prompt fulfills that

> To check if there is something to read at this very moment, you
> can use any of the following methods:

Thanks for all the suggestions! That is just what I needed.

>   -
>   - the FIONREAD ioctl (the ioctl() function lives in the fcntl
>     module, and the FIONREAD constant is in the termios module)
>   - set the underlying file descriptor in non-blocking mode:
>         flags = fcntl.fcntl(fd, fcntl.F_GETFL)
>         fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NDELAY)
>     After that, reads on the pipe will raise an IOError exception
>     with the errorcode EWOULDBLOCK.

That sounds like the simplest approach.

>   - start a thread that does blocking reads from the pipe, and
>     puts the chunks it reads on a queue for your main thread to
>     grab.

Yes but my python threading is worse than rudimentary. I will look
into the `trheading` module suggested by the other poster.

> For the last approach, you might be interested in my asyncproc
> module, which does exactly that.  You can download it from
> <>.

OK, I'll look into that, too.

Thanks again,

-- O.L.

More information about the Python-list mailing list