bufsize in subprocess

Discussion in 'Python' started by yves@zioup.com, Jan 22, 2012.

  1. Guest

    Is this the expected behaviour?
    When I run this script, it reads only once, but I expected once per line with
    bufsize=1.

    What I am trying to do is display the output of a slow process in a tkinter
    window as it runs. Right now, the process runs to completion, then display the
    result.

    import subprocess

    com = ['/bin/ls', '-l', '/usr/bin']
    with subprocess.Popen(com, bufsize=1, stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT) as proc:
    print('out: ' + str(proc.stdout.read(), 'utf8'))


    Thanks.

    --
    Yves. http://www.SollerS.ca/
    http://ipv6.SollerS.ca
    http://blog.zioup.org/
     
    , Jan 22, 2012
    #1
    1. Advertising

  2. Chris Rebert Guest

    On Sat, Jan 21, 2012 at 9:45 PM, <> wrote:
    > Is this the expected behavior?


    Yes. `.read()` [with no argument] on a file-like object reads until
    EOF. See http://docs.python.org/library/stdtypes.html#file.read

    > When I run this script, it reads only once, but I expected once per line
    > with bufsize=1.


    You want proc.stdout.readline().
    http://docs.python.org/library/stdtypes.html#file.readline

    > What I am trying to do is display the output of a slow process in a tkinter
    > window as it runs. Right now, the process runs to completion, then display
    > the result.
    >
    >    import subprocess
    >
    >    com = ['/bin/ls', '-l', '/usr/bin']
    >    with subprocess.Popen(com, bufsize=1, stdout=subprocess.PIPE,
    > stderr=subprocess.STDOUT) as proc:
    >        print('out: ' + str(proc.stdout.read(), 'utf8'))


    Cheers,
    Chris
     
    Chris Rebert, Jan 22, 2012
    #2
    1. Advertising

  3. Guest

    On 2012-01-22 00:27, Chris Rebert wrote:
    > On Sat, Jan 21, 2012 at 9:45 PM,<> wrote:
    >> Is this the expected behavior?

    >
    > Yes. `.read()` [with no argument] on a file-like object reads until
    > EOF. See http://docs.python.org/library/stdtypes.html#file.read


    Right, got it now.

    > You want proc.stdout.readline().


    Yes, or a for loop, this works for me now:


    import subprocess

    com = ['/bin/ls', '-l', '/usr/bin']
    with subprocess.Popen(com, stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT) as proc:
    for line in proc:
    print('out: ' + str(line, 'utf8'))



    --
    Yves. http://www.SollerS.ca/
    http://ipv6.SollerS.ca
    http://blog.zioup.org/
     
    , Jan 22, 2012
    #3
  4. Guest

    , Jan 22, 2012
    #4
    1. Advertising

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. Thomas =?ISO-8859-15?Q?G=FCttler?=

    Maxmium bufsize using open2?

    Thomas =?ISO-8859-15?Q?G=FCttler?=, Jul 10, 2003, in forum: Python
    Replies:
    2
    Views:
    350
    Piet van Oostrum
    Jul 12, 2003
  2. Jonathan Hudgins

    popen bufsize not allowed on windows

    Jonathan Hudgins, Jul 24, 2004, in forum: Python
    Replies:
    1
    Views:
    363
    Jonathan Hudgins
    Jul 25, 2004
  3. I. Myself
    Replies:
    2
    Views:
    487
    I. Myself
    Apr 30, 2006
  4. =?iso-8859-1?B?aWFuYXLp?=

    real time updating of popen, bufsize=0 problems

    =?iso-8859-1?B?aWFuYXLp?=, Apr 6, 2007, in forum: Python
    Replies:
    4
    Views:
    576
    =?iso-8859-1?B?aWFuYXLp?=
    Apr 6, 2007
  5. Steve Holden
    Replies:
    0
    Views:
    781
    Steve Holden
    Feb 8, 2009
Loading...

Share This Page