Re: read stdout/stderr without blocking

Discussion in 'Python' started by Adriaan Renting, Sep 12, 2005.

  1. Check out the select module, for an example on how to use it:
    pexpect.sourceforge.net




    >>>Jacek Pop*awski <> 09/12/05 10:07 am >>>

    Popen from subprocess module gives me access to stdout, so I can read
    it. Problem is, that I don't know how much data is available... How can
    I read it without blocking my program?

    example:
    --------------------------------------------------------------------
    import subprocess
    import time

    command="ls -l -R /"

    p=subprocess.Popen(command,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)

    while (p.poll()==None):
    print "."
    r=p.stdout.read()
    --------------------------------------------------------------------

    when you comment out read() - you will notice that loop is working, with
    read() loop is blocked
    Of course I don't need to read() inside loop, but... if output is very
    long (like from "make") and I don't read from stdout - command will
    block itself! I tried to increase bufsize, but it didn't help.

    Is there a way to read only available data from stdout/stderr?
    Is there a way to not block Popen command without reading stdout/stderr?
    --
    http://mail.python.org/mailman/listinfo/python-list
     
    Adriaan Renting, Sep 12, 2005
    #1
    1. Advertisements

  2. Adriaan Renting wrote:
    > Check out the select module, for an example on how to use it:
    > pexpect.sourceforge.net


    Two problems:
    - it won't work on Windows (Cygwin)
    - how much data should I read after select? 1 character? Can it block if
    I read 2 characters?
     
    =?UTF-8?B?SmFjZWsgUG9wxYJhd3NraQ==?=, Sep 12, 2005
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon Landenburer

    redirecting stderr and stdout

    Jon Landenburer, May 12, 2004, in forum: Perl
    Replies:
    1
    Views:
    11,512
    Joe Smith
    May 13, 2004
  2. ladygrinningsoul

    Reading stdout and stderr separately

    ladygrinningsoul, Nov 18, 2004, in forum: Perl
    Replies:
    3
    Views:
    1,181
    Alexey A. Kiritchun
    Apr 25, 2005
  3. VisionSet
    Replies:
    6
    Views:
    9,949
    Sudsy
    Sep 20, 2003
  4. =?ISO-8859-2?Q?Jacek_Pop=B3awski?=

    read stdout/stderr without blocking

    =?ISO-8859-2?Q?Jacek_Pop=B3awski?=, Sep 12, 2005, in forum: Python
    Replies:
    1
    Views:
    4,094
    =?ISO-8859-2?Q?Jacek_Pop=B3awski?=
    Sep 13, 2005
  5. Adriaan Renting

    Re: read stdout/stderr without blocking

    Adriaan Renting, Sep 12, 2005, in forum: Python
    Replies:
    10
    Views:
    784
    =?ISO-8859-2?Q?Jacek_Pop=B3awski?=
    Sep 16, 2005
  6. Serge Savoie
    Replies:
    4
    Views:
    556
    Serge Savoie
    Oct 1, 2008
  7. George C. Demetros

    BLOCKING and STDOUT/STDERR

    George C. Demetros, Jan 6, 2004, in forum: Perl Misc
    Replies:
    1
    Views:
    240
    Ben Morrow
    Jan 6, 2004
  8. billy
    Replies:
    3
    Views:
    308
Loading...