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


Groups > comp.lang.python > #10812

Re: Snippet: The leanest Popen wrapper

From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: Snippet: The leanest Popen wrapper
Followup-To comp.lang.python
Date 2011-08-03 18:21 +0200
Organization None
Message-ID <j1bsi2$8ro$1@solani.org> (permalink)
References <51b2d157-3fea-4f8e-80b4-e7142629eca8@s21g2000pre.googlegroups.com>

Followups directed to: comp.lang.python

Show all headers | View raw


Phlip wrote:

> Groupies:

I smell a slight misperception of the audience you are addressing ;)

> This is either a code snippet, if you like it, or a request for a
> critique, if you don't.
> 
> I want to call a command and then treat the communication with that
> command as an object. And I want to do it as application-specifically
> as possible. Anyone could think of a way to productize this:
> 
> def command(*cmdz):
> 
>     process = Popen( flatten(cmdz),
>                      shell= True,
>                      stdout= subprocess.PIPE,
>                      stderr= subprocess.PIPE,
>                      bufsize= 4096 )

Protect your environment, don't let stderr pollute the nearby river ;)

>     def line():
>         return process.stdout.readline().rstrip()
> 
>     def s():
>         while True:
>             l = line()

At that point l may be empty because you have read the output completely or 
because there was an empty line that you rstripped to look like the end of 
file.

>             if not l:  break
>             yield l
> 
>     line.s = s
> 
>     return line

I think you are overdoing that closure/function factory thing a bit...

Seriously, you should reread the subprocess documentation to learn how to 
avoid deadlocks.

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


Thread

Snippet: The leanest Popen wrapper Phlip <phlip2005@gmail.com> - 2011-08-03 08:29 -0700
  Re: Snippet: The leanest Popen wrapper Peter Otten <__peter__@web.de> - 2011-08-03 18:21 +0200
  Re: Snippet: The leanest Popen wrapper Thomas Jollans <t@jollybox.de> - 2011-08-03 18:39 +0200
  Re: Snippet: The leanest Popen wrapper Chris Rebert <clp2@rebertia.com> - 2011-08-03 10:27 -0700
    Re: Snippet: The leanest Popen wrapper Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-08-04 10:10 +0200
      Re: Snippet: The leanest Popen wrapper Chris Rebert <clp2@rebertia.com> - 2011-08-04 01:42 -0700
        Re: Snippet: The leanest Popen wrapper Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-08-04 11:25 +0200
  Re: Snippet: The leanest Popen wrapper Phlip <phlip2005@gmail.com> - 2011-08-03 11:04 -0700
  Re: Snippet: The leanest Popen wrapper Terry Reedy <tjreedy@udel.edu> - 2011-08-03 16:20 -0400
  Re: Snippet: The leanest Popen wrapper Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-08-04 10:24 +0200
  Re: Snippet: The leanest Popen wrapper Yves-Gwenael Bourhis <ybourhis@distroiwork4.com> - 2011-08-04 15:19 +0200

csiph-web