Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > bufsize in subprocess

Reply
Thread Tools

bufsize in subprocess

 
 
yves@zioup.com
Guest
Posts: n/a
 
      01-22-2012
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/
 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      01-22-2012
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/stdty...#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
 
Reply With Quote
 
 
 
 
yves@zioup.com
Guest
Posts: n/a
 
      01-22-2012
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/
 
Reply With Quote
 
yves@zioup.com
Guest
Posts: n/a
 
      01-22-2012

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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Quetion about flags of socket.recv(bufsize, [flags]) Steve Holden Python 0 02-08-2009 04:09 PM
real time updating of popen, bufsize=0 problems =?iso-8859-1?B?aWFuYXLp?= Python 4 04-06-2007 08:07 PM
Why does bufsize=1 not work in subprocess.Popen ? I. Myself Python 2 04-29-2006 11:05 PM
popen bufsize not allowed on windows Jonathan Hudgins Python 1 07-25-2004 04:14 AM
Maxmium bufsize using open2? Thomas =?ISO-8859-15?Q?G=FCttler?= Python 2 07-12-2003 11:00 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57