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


Groups > comp.lang.python > #90579

use of subprocess module inside generator

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <p-santoro@sbcglobal.net>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'output': 0.05; 'root': 0.05; 'none:': 0.07; 'advance': 0.07; 'below)': 0.09; 'caller': 0.09; 'converted': 0.09; 'expected.': 0.09; 'here?': 0.09; 'pyodbc': 0.09; 'statements': 0.09; 'subject:module': 0.09; 'python': 0.11; 'def': 0.12; 'windows': 0.15; '(via': 0.16; 'function).': 0.16; 'generator.': 0.16; "microsoft's": 0.16; 'received:66.196': 0.16; 'shifts': 0.16; 'stdout': 0.16; 'subject:generator': 0.16; 'looked': 0.18; 'module': 0.19; 'file,': 0.19; 'command': 0.22; 'appears': 0.22; 'code,': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; "i've": 0.25; '(see': 0.26; 'this:': 0.26; 'function': 0.29; 'quickly': 0.29; 'change,': 0.30; 'returned': 0.30; "i'm": 0.30; 'fine,': 0.31; 'minor': 0.31; 'skip:_ 10': 0.34; 'received:66': 0.35; 'skip:u 20': 0.35; 'something': 0.35; 'accessing': 0.36; 'module.': 0.36; 'yield': 0.36; 'next': 0.36; 'server': 0.38; 'thank': 0.38; 'to:addr:python-list': 0.38; 'issue': 0.38; 'to:addr:python.org': 0.39; 'called': 0.40; 'easy': 0.60; 'future': 0.60; 'around.': 0.60; 'received:bf1.yahoo.com': 0.60; 'received:mail.bf1.yahoo.com': 0.60; 'new': 0.61; 'simply': 0.61; 'skip:n 10': 0.64; 'hours': 0.66; 'latest': 0.67; 'header:Reply- To:1': 0.67; 'reply-to:no real name:2**0': 0.71; 'received:bullet.mail.bf1.yahoo.com': 0.74; 'goal': 0.75; 'hoping': 0.75; 'behavior': 0.77; '100%': 0.77; 'directed': 0.83; 'flexible,': 0.84; 'apparent': 0.91; 'received:98.138.226': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=sbcglobal.net; s=s2048; t=1431546201; bh=i91+R3vZMZTAdCdRpqqqKgu/Z3BD60ygoy4kSr2cw7Y=; h=Date:From:Reply-To:To:Subject:From:Subject; b=KdrghTB9hZdQ4wWX6xUj+OmuLOKsZ3+97hNDHo7Z0OzNRkYU/trVAKT2i/MIMjfwGO2tHMmROju5LBuntMDwqml2PuI+kOaYa0/+On+YwKI/VwL3XEeRKVgLhBQK9mbISjNU28gJV4nbPXiKWktKI+9uGJhiS9fCwuHap1azTcpBIESdj5cweAMwqSEtSerFy8xnwhg0czJ91v9A3TtNZFIx65aH5lbVfvnfjaSWgMyv9KsU1T3DqjeqvsNd8Ov7lBX/4BytKNwCmbS6eOgE/LJrUhnBfEi/O9NEbhrCrLqXJONh3pHDC9wxXxvAA4ItSFKOWID0csO3xrGvYKlhHA==
X-Yahoo-Newman-Id 877597.71505.bm@smtp114.sbc.mail.ne1.yahoo.com
X-Yahoo-Newman-Property ymail-3
X-YMail-OSG Ww6JJkMVM1lUwS_btJdALF9bbdM9W0Z2E05eeJdeee7NEWk KtM7b27cWt5tN6zbn6U0lubYe_SWY2TExZ5goaD4YXLiBKiHjT.2FTuuMe7K NbCgnKCrNbG6f9ORHSDJkk3hIDzCcxqxyW32dm4aS2q9yLcZYttzXsyk3kkL sG4utuhjPCp2d2CxS6pu9vV59vzRWOPX5dVnbBOFxpxcM162HXFKYxXqSFiA UmNMAcrPBGBXuDasGKIJLmlq.UULQlVWCV35gXRQgRPFwEyE0Z4HwgMB_oXY 1zvOKu3wBW4vovb1zvPZLWXDl0Rvy6TUvwsvbBaoYqp2EodSfqMGmZPtcD6Y svl8IT0IINBN8HKvucWANsotN7VnH_4wrGlGwc7NwHhwDgLDBqsCpD1LiLD7 Oz83Og_wt1bEoTppjsav997jme4NoIzqpvqc3OENeFTs0ttviph7aaYMk07_ z79R0k4JQiYYOaZ2fzpVtO18muM1BJfOxHeFFFbYWWUH7bQeb1P9j5yphMk4 ttn4ldFc267Cc.3CdC5GBZZrZ6yvQzYl6QGZ1U3VMKdUnIEMJchPSe0fU
X-Yahoo-SMTP PcRdH1CswBDjJunwicc5apkCQkeGfezTDkgQJyBKPzChimw2
Date Wed, 13 May 2015 15:43:21 -0400
From Peter <p-santoro@sbcglobal.net>
Reply-To peter@psantoro.net
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0
MIME-Version 1.0
To python-list@python.org
Subject use of subprocess module inside generator
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
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.458.1431546353.12865.python-list@python.org> (permalink)
Lines 51
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1431546353 news.xs4all.nl 2889 [2001:888:2000:d::a6]:34309
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:90579

Show key headers only | View raw


I'm using Python 3.4.3 on Windows 7 (with latest patches) to develop a 
sqlcmd module for accessing SQL Server (via Microsoft's sqlcmd.exe).  My 
goal is to develop a 100% Python 3 module that's easy to use, flexible, 
and by design shifts the majority of future SQL Server Python database 
access maintenance to Microsoft.  So far, I've successfully and quickly 
converted a few of my Python pyodbc applications/tools to use this new 
module.  However, I recently ran into an apparent Python issue which 
cost me a few hours to diagnose and work around.

I'm hoping that someone might know what the root cause of my issue was. 
  Perhaps I've hit a bug/restriction with Python generators?

My original generator function looked like this:

def _raw_data(cl, stdout, *, opath=None, timeout=timeout):
     stdout = subprocess.check_output(cl, universal_newlines=True, 
timeout=timeout)

     if opath is None:
         for line in stdout.splitlines():
             yield line.strip()
     else:
         with open(opath) as f:
             for line in f:
                 yield line.strip()

The above function appeared to work fine, if the command line directed 
sqlcmd.exe to send its output to stdout.  However, if the command line 
directed sqlcmd.exe to send its output to a file, 
subprocess.check_output would never be called when next was called on 
the returned generator.  I verified this behavior with print statements 
inside my code, as well as, inside the subprocess module.

My work around was to simply move the call to subprocess.check_output 
outside of the generator function (see below) to its caller (a 
non-generator function).  With this minor change, everything appears to 
work as expected.  OK, so am I missing something here?

def _raw_data(stdout, *, opath=None):
     if opath is None:
         for line in stdout.splitlines():
             yield line.strip()
     else:
         with open(opath) as f:
             for line in f:
                 yield line.strip()

Thank you in advance for your assistance.

Peter Santoro

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


Thread

use of subprocess module inside generator Peter <p-santoro@sbcglobal.net> - 2015-05-13 15:43 -0400

csiph-web