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


Groups > comp.lang.python > #97317

Re: Only getting the first 6 lines

Path csiph.com!news.swapon.de!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail
Return-Path <cameron@cskk.homeip.net>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.013
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'binary': 0.05; 'subject:getting': 0.07; 'fetch': 0.09; 'sucks': 0.09; 'output': 0.13; "skip:' 30": 0.15; '1):': 0.16; 'decode': 0.16; 'file).': 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'in-memory': 0.16; 'lineno': 0.16; 'lineno,': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'simpson': 0.16; 'slow,': 0.16; 'subprocess': 0.16; 'wrote:': 0.16; 'memory': 0.17; 'skip:l 30': 0.18; '(the': 0.22; "skip:' 40": 0.22; 'terminate': 0.22; 'cheers,': 0.22; 'skip:( 40': 0.23; 'split': 0.23; 'this:': 0.23; 'import': 0.24; 'header:In-Reply- To:1': 0.24; 'header:User-Agent:1': 0.26; 'command': 0.26; "i'm": 0.30; 'print': 0.30; 'especially': 0.32; 'run': 0.33; 'this?': 0.34; 'could': 0.35; 'something': 0.35; 'but': 0.36; 'instead': 0.36; 'there': 0.36; 'lines': 0.36; 'depends': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'charset:us-ascii': 0.37; 'log': 0.38; 'mean': 0.38; 'data': 0.39; 'does': 0.39; 'subject:the': 0.39; 'enough': 0.39; 'skip:e 20': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'your': 0.60; 'programs': 0.62; 'more': 0.63; 'six': 0.65; 'cameron': 0.66; 'received:61': 0.72; '>from': 0.76; 'completion': 0.79; 'cecil': 0.84; 'streams': 0.84; 'westerhof': 0.84; 'ps.': 0.91; 'subject:Only': 0.91; 'miracle': 0.93
X-Authentication-Info Submitted using ID cskk@bigpond.com
X-Authority-Analysis v=2.0 cv=IpacgcDg c=1 sm=1 a=W+dVm6qrCSyjJUZzXQU3Kw==:17 a=vrnE16BAAAAA:8 a=ZtCCktOnAAAA:8 a=5lJygRwiOn0A:10 a=Gmn0_V3Hf6r-II93ehwA:9 a=CjuIK1q_8ugA:10 a=W+dVm6qrCSyjJUZzXQU3Kw==:117
Date Fri, 2 Oct 2015 08:50:32 +1000
From Cameron Simpson <cs@zip.com.au>
To python-list@python.org
Subject Re: Only getting the first 6 lines
MIME-Version 1.0
Content-Type text/plain; charset=us-ascii; format=flowed
Content-Disposition inline
In-Reply-To <87y4fmp7xi.fsf@Equus.decebal.nl>
User-Agent Mutt/1.5.23 (2014-03-12)
References <87y4fmp7xi.fsf@Equus.decebal.nl>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.321.1443740825.28679.python-list@python.org> (permalink)
Lines 47
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1443740825 news.xs4all.nl 23732 [2001:888:2000:d::a6]:56176
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:97317

Show key headers only | View raw


On 01Oct2015 23:58, Cecil Westerhof <Cecil@decebal.nl> wrote:
>I want to get the first 6 lines of ps output. For this I use:
>========================================================================
>from subprocess import check_output
>
>ps_command = ('ps', '-eo', 'user,pid,pcpu,pmem,stat,start,time,cmd', '--sort')
>message = '\n'.join(check_output(ps_command + ('-%cpu',)).decode("utf-8").splitlines()[0:6])
>========================================================================
>
>It works, but does not look very efficient. Is there a better way to
>do this?

It depends what you mean by inefficient. I'm presuming you mean that this:

  - sucks in all the output of ps instead of just the first 6 lines

  - sucks all the output into memory instead of just some of it

  - does more in-memory work in splitlines()

  - needs ps to run to completion instead of just long enough to print 6 lines

You could just read six lines of output from ps. Something like this 
(untested):


  lines = []
  for lineno, line in \
        enumerate(Popen(ps_command + ('-%cpu',),
                        stdin=NULL, stdout=PIPE).stdout,
                  1):
    lines.append(line.decode("utf-8"))
    if lineno >= 6:
      break

This works because in a miracle of foresight you can split binary data streams 
(the stdout) on line breaks, so you can fetch "binary" lines and decode them 
individually.

This approach is important if the output of your command is large or slow, as 
it does not need to suck the whole thing into memory or to wait for it to 
finish a long procedure. With "ps" these issues are pretty minor; with some 
other programs it can be significant, especially if the other program _doesn't_ 
terminate (consider "tail -f" of an active log file).

Cheers,
Cameron Simpson <cs@zip.com.au>

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Only getting the first 6 lines Cecil Westerhof <Cecil@decebal.nl> - 2015-10-01 23:58 +0200
  Re: Only getting the first 6 lines Ian Kelly <ian.g.kelly@gmail.com> - 2015-10-01 16:58 -0600
  Re: Only getting the first 6 lines Cameron Simpson <cs@zip.com.au> - 2015-10-02 08:50 +1000
    Re: Only getting the first 6 lines Cecil Westerhof <Cecil@decebal.nl> - 2015-10-02 13:08 +0200
  Re: Only getting the first 6 lines Peter Otten <__peter__@web.de> - 2015-10-02 09:37 +0200
    Re: Only getting the first 6 lines Cecil Westerhof <Cecil@decebal.nl> - 2015-10-02 13:11 +0200

csiph-web