Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #19205 > unrolled thread

bufsize in subprocess

Started byyves@zioup.com
First post2012-01-21 22:45 -0700
Last post2012-01-22 10:05 -0700
Articles 4 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  bufsize in subprocess yves@zioup.com - 2012-01-21 22:45 -0700
    Re: bufsize in subprocess Chris Rebert <clp2@rebertia.com> - 2012-01-21 23:27 -0800
      Re: bufsize in subprocess yves@zioup.com - 2012-01-22 09:43 -0700
        Re: bufsize in subprocess yves@zioup.com - 2012-01-22 10:05 -0700

#19205 — bufsize in subprocess

Fromyves@zioup.com
Date2012-01-21 22:45 -0700
Subjectbufsize in subprocess
Message-ID<koNSq.6594$sA3.1810@newsfe01.iad>
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/

[toc] | [next] | [standalone]


#19207

FromChris Rebert <clp2@rebertia.com>
Date2012-01-21 23:27 -0800
Message-ID<mailman.4920.1327217278.27778.python-list@python.org>
In reply to#19205
On Sat, Jan 21, 2012 at 9:45 PM,  <yves@zioup.com> 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

[toc] | [prev] | [next] | [standalone]


#19221

Fromyves@zioup.com
Date2012-01-22 09:43 -0700
Message-ID<11XSq.1434$om1.1057@newsfe12.iad>
In reply to#19207
On 2012-01-22 00:27, Chris Rebert wrote:
> On Sat, Jan 21, 2012 at 9:45 PM,<yves@zioup.com>  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/

[toc] | [prev] | [next] | [standalone]


#19224

Fromyves@zioup.com
Date2012-01-22 10:05 -0700
Message-ID<FlXSq.5765$WY2.4281@newsfe13.iad>
In reply to#19221
  import subprocess

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

-- 
Yves.                                                  http://www.SollerS.ca/
                                                        http://ipv6.SollerS.ca
                                                        http://blog.zioup.org/

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web