How to perform a nonblocking read from a process

Nick Craig-Wood nick at craig-wood.com
Wed Jun 4 10:30:21 EDT 2008


rdabane at gmail.com <rdabane at gmail.com> wrote:
>  Hi,
>  I'm trying to perform following operation from inside the python
>  script
>  1. Open a shell ( start a process )
>  2. Send command1 to the process
>  3. Get output from the process
>  4. Send command2 to the process
>  5. Get output from the process
>  ......
> 
> 
>  Following is sample code :
> 
>  from subprocess import *
>  p2 = Popen('python',stdin=PIPE,stdout=PIPE,universal_newlines=True)
>  for i in range(10):
>      p2.stdin.write('print 10'+'\n')
>      o,e = p2.stdout.readline()
>      print o,e
> 
>  It seems that stdout.readline() is a blocking read and it just gets
>  stuck their..
>  How to fix this ..

If you are working under linux/some-unix-like-os then use the python
expect module which is for exactly this sort of thing.

  http://www.noah.org/wiki/Pexpect

  Pexpect is a pure Python expect-like module. Pexpect makes Python a
  better tool for controlling other applications.

  Pexpect is a pure Python module for spawning child applications;
  controlling them; and responding to expected patterns in their
  output. Pexpect works like Don Libes' Expect. Pexpect allows your
  script to spawn a child application and control it as if a human
  were typing commands.

  Pexpect can be used for automating interactive applications such as
  ssh, ftp, passwd, telnet, etc. It can be used to a automate setup
  scripts for duplicating software package installations on different
  servers. It can be used for automated software testing. Pexpect is
  in the spirit of Don Libes' Expect, but Pexpect is pure
  Python. Unlike other Expect-like modules for Python, Pexpect does
  not require TCL or Expect nor does it require C extensions to be
  compiled. It should work on any platform that supports the standard
  Python pty module. The Pexpect interface was designed to be easy to
  use.

You'll never get it to work with subprocess like this because of the
buffering.

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



More information about the Python-list mailing list