Bug/Weak Implementation? popen* routines can't handle simultaneous read/write?

Nick Craig-Wood nick at craig-wood.com
Fri Jun 8 12:30:04 EDT 2007


Noah <noah at noah.org> wrote:
>  On Jun 7, 9:01 am, dmoore <damienlmo... at gmail.com> wrote:
> 
>  popen and friends will never do what you want it to do. Down that path
>  lies bitter disappointment.
>  You need pseduo-ttys and non-blocking IO. I don't know how to do this
>  on Windows, but I know it's possible.
>  Cygwin does it.
> 
> > Anybody have any thoughts on this? Do I have my story straight? (the
> > popen variants can't handle this case and there are no other
> > alternatives in the standard python distro) Is there some place I can
> > submit this as a feature request? (Python dev?)
> 
>  Try Pexpect http://pexpect.sourceforge.net/
>  It's been around for a long time and is quite complete and stable.
> 
>  The catch is that it's UNIX-only. If you want to tease out the magic
>  code from wxProcess that
>  does non-blocking reads on win32 then I'd be happy to integrate that
>  into the current development branch
>  of Pexpect.

Windows has a really strange idea of non-blocking IO - it uses
something called overlapped io.  You or in the FILE_FLAG_OVERLAPPED
flag when you create the file/pipe.  You then pass in overlap buffers
for reading writing.

I implemented this for serial ports in C a while ago.  If you look at
the code in pyserial (in serialwin32.py) you'll see a very similar
implementation of non blocking IO.  I assume that the same things will
work for pipes, but I've only direct experience with serial ports!

>  I'm sure this is feasible without any C extensions -- it might require
>  some ctypes hacking.

pyserial uses win32file and win32event

>  I know Windows has pretty decent async IO, but I don't know what they
>  have as an equivalent for a pty.
>  Maybe it isn't necessary. A pty is only necessary on UNIX because the
>  standard c library, stdio, behaves
>  differently when it's talking to a plain pipe versus a terminal -- it
>  switches buffering
>  between block and line oriented buffer. You don't want block buffering
>  on interactive applications.

Pty's probably aren't needed on Windows.

BTW I'd love to see pexpect working on windows and also in the
standard library.  It is the proper answer to controlling other
interactive processes IMHO.

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list