Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: Question about asyncio and blocking operations Date: Tue, 26 Jan 2016 08:44:33 -0700 Lines: 29 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de hcoba3XfiOkRSdofs0hxSA5HvoWoiqa/70A1doD1Opag== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'received:209.85.223': 0.03; 'subject:Question': 0.05; 'caller': 0.07; 'cursor': 0.09; 'iterate': 0.09; 'requested.': 0.09; 'rows': 0.09; 'jan': 0.11; 'def': 0.13; '2016': 0.16; 'iterator': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'row': 0.16; 'wrote:': 0.16; 'basically': 0.18; 'am,': 0.23; 'bit': 0.23; 'seems': 0.23; 'examples': 0.24; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'message- id:@mail.gmail.com': 0.27; 'yield': 0.27; 'another.': 0.29; 'blocking': 0.29; 'url:peps': 0.29; 'probably': 0.31; 'possibly': 0.32; 'run': 0.33; 'url:python': 0.33; 'usually': 0.33; 'right?': 0.33; 'wrap': 0.33; 'tue,': 0.34; 'that,': 0.34; 'received:google.com': 0.35; 'url:dev': 0.35; "isn't": 0.35; 'but': 0.36; 'should': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'list.': 0.37; 'received:209': 0.38; 'does': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'future': 0.60; 'provide': 0.61; 'here.': 0.62; 'making': 0.62; 'fact,': 0.67; 'worth': 0.67; '26,': 0.72; 'frank': 0.72; 'sound': 0.72; 'mention.': 0.84; 'to:name:python': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=G/T/WWl1gd+YfHtwjk+UCITOhToFz46ixBboMD+t5XU=; b=cYo+R3pUa9KLY9GHd+gyj1TmRMVs5qFhEcFg/Ib5FaOG/rU2v0VyhtpGr4KADyzGyX IiwfZ+A5sG0SWzGEnNmPVzwxvFUFVTyDAohPbf9uWwonYVRjMw3h6DtpXI279Z0T8J24 OPFtGzP7fgInZ6yCLXuTojo7FvMMu9z1AtOl6wEuDRFZIp4VmSG6BrVop8c2jN35pjHz INiSWNNjbLw2m9sLQTB4pg/5fKbdGs+VDzfreIR6N4Acrl9/TCBzK5wdJsSfZFzeWZWJ I2rJVqBFT0YTuUjKObB2hZ9CWACWFz5WDiBTEsh3fYgwAOKg2bfWHyVxVSPbzx1LuqmG MTEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type; bh=G/T/WWl1gd+YfHtwjk+UCITOhToFz46ixBboMD+t5XU=; b=Jx37sAx5hLEA3/IsIbV8x181Z8exwW39WT5sJkJ0lNfiqy2GmjpY5nfHv1svb1VNg+ w2gOA+7he+gLxEN5KBOp7VC2fol2w4xfY7CtjbTm7CPPeQ58Nr/XbPiRM8fMDcASKrrN oZutmi+6eYts5FmY86AWYaoBML+Otr2gW3iNGiz5Ni+1s6vqH3QiBZZCSMHE+hSFKJup yoGk9Vwqz8OqaIhzCEaV8e7Wieehsw4Q2LlvKTdwYXq39ktZYtdcj7wrpzMDOCZya+Ri PW+R6w31ESV4l3F66Qh9SuR2BEVsD1YijWTZwPhWCkXca/Ep0TNUHpeYeGAKbPIMykVS oCgA== X-Gm-Message-State: AG10YOSJVOcrescGkOTO90llX3mmP3+ezHK7Jo/rFbM39ZiHkWnWXVXk3maHjjb+FSd9wK8OJs34hl/NWWR+6Q== X-Received: by 10.107.19.30 with SMTP id b30mr26976305ioj.11.1453823113046; Tue, 26 Jan 2016 07:45:13 -0800 (PST) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:102124 On Tue, Jan 26, 2016 at 7:15 AM, Frank Millman wrote: > I am making some progress, but I have found a snag - possibly unavoidable, > but worth a mention. > > Usually when I retrieve rows from a database I iterate over the cursor - > > def get_rows(sql, params): > cur.execute(sql, params) > for row in cur: > yield row > > If I create a Future to run get_rows(), I have to 'return' the result so > that the caller can access it by calling future.result(). > > If I return the cursor, I can iterate over it, but isn't this a blocking > operation? As far as I know, the DB adaptor will only actually retrieve the > row when requested. > > If I am right, I should call fetchall() while inside get_rows(), and return > all the rows as a list. > > This seems to be swapping one bit of asynchronicity for another. > > Does this sound right? You probably want an asynchronous iterator here. If the cursor doesn't provide that, then you can wrap it in one. In fact, this is basically one of the examples in the PEP: https://www.python.org/dev/peps/pep-0492/#example-1