Non-blocking read with popen subprocess

Javier Collado javier.collado at gmail.com
Fri Jul 31 04:27:01 EDT 2009


Hello,

According to my experience and from what I've read in other threads,
subprocess isn't easy to use for interactive tasks. I don't really
know, but maybe it wasn't even designed for that at all.

On the other hand, pexpect seems to work fine for interactive use and
even provides a method for nonblocking reads, so I think that you
should consider to take a look at it:
http://pexpect.sourceforge.net/pexpect.html#spawn-read_nonblocking

Best regards,
    Javier

2009/7/31 Dhanesh <dhaneshpai at gmail.com>:
> Hi ,
>
> I am trying to use subprocess popen on a windows command line
> executable with spits messages on STDOUT as well as STDIN. Code
> snippet is as below :-
> ##########################################################################
> sOut=""
>    sErr=""
>    javaLoaderPath = os.path.join("c:\\","Program Files","Research In
> Motion","BlackBerry JDE 4.7.0","bin","Javaloader.exe")
>    cmd = [javaLoaderPath,'-u','load','helloworld.jad']
>    popen = subprocess.Popen
> (cmd,bufsize=256,shell=False,stdout=subprocess.PIPE,
> stderr=subprocess.PIPE)
>    while True:
>        sErr = sErr + popen.stderr.read(64)
>        sOut = sOut + popen.stdout.read(64)------------------> Blocks
> here
>        if None != popen.poll():
>            break;
> ##########################################################################
>
> I observed that python scripts blocks at stdout read on the other hand
> when I do not create a child stdout PIPE  say  " stdout=None" , things
> seems to be working fine.
>
> how can I we have a non blocking read ?
> And why does stdout block even where data is available to be read
> ( which seem to be apparent when stdout=None, and logs appear on
> parents STDOUT) ?
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list