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


Groups > comp.lang.python > #103543

Re: Looking for examples of using aiopg with aiohttp and non-async startup.

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Ray Cote <rgacote@appropriatesolutions.com>
Newsgroups comp.lang.python
Subject Re: Looking for examples of using aiopg with aiohttp and non-async startup.
Date Fri, 26 Feb 2016 09:52:12 -0500
Lines 66
Message-ID <mailman.153.1456498366.20994.python-list@python.org> (permalink)
References <CAG5tnzrGFexYKaC6YhMaP-_vwudMgxwGNibh19DWmKLQMqp9vQ@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
X-Trace news.uni-berlin.de hobErtWfEHmGmbn4eMY2gwj15yyearhSGteezrvWdexQ==
Return-Path <rgacote@appropriatesolutions.com>
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; 'handler': 0.04; 'startup': 0.05; '__name__': 0.07; 'cursor': 0.09; 'loop.': 0.09; 'subject:using': 0.09; 'way:': 0.09; '\xe2\x80\x94': 0.09; 'def': 0.13; 'thu,': 0.15; '2016': 0.16; 'async': 0.16; 'doesn\xe2\x80\x99t': 0.16; 'handler)': 0.16; 'hello:': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'refreshing': 0.16; 'subject:non': 0.16; 'wrote:': 0.16; 'app': 0.16; 'skip:l 30': 0.18; 'seems': 0.23; 'feb': 0.23; 'interfaces': 0.23; 'header:In- Reply-To:1': 0.24; 'example': 0.26; 'separate': 0.27; 'question': 0.27; 'message-id:@mail.gmail.com': 0.27; 'correct': 0.28; 'looks': 0.29; 'ray': 0.29; 'code:': 0.29; 'starts': 0.29; 'connection': 0.30; 'raymond': 0.30; 'run': 0.33; 'received:google.com': 0.35; 'integrate': 0.35; 'but': 0.36; 'project': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'to:addr :python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'skype:': 0.38; 'someone': 0.38; 'subject:-': 0.39; 'to:addr:python.org': 0.40; 'subject:with': 0.40; 'provide': 0.61; 'email:': 0.62; 'skip:w 40': 0.66; 'skip:\xe2 10': 0.70; '8bit%:40': 0.72; '8bit%:46': 0.76; 'await': 0.76; 'application?': 0.84; 'kicking': 0.84; 'we\xe2\x80\x99re': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=appropriatesolutions.com; s=google; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=0VW1qs0EDi1KX3zRV9tearUE/RacAQ7H4SjBZqJnbdY=; b=fSyVMobJNgrI2gvsPsrTzuMi3OO2koziG64CRj/fosjtW2MEd+24dc93uC4fkSozVh WVmaf28c964mM1EeAB7E21YT55iet5sneZEqkh5NgD7U8ch2lZ0Hy0Kz/utGTPCVv39T 1JjaHQs+XEQfk6AYsvy7JqYVVxv1iLB5LN6Yo=
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; bh=0VW1qs0EDi1KX3zRV9tearUE/RacAQ7H4SjBZqJnbdY=; b=c5fkISzQUPjb7D5CVzqHtF3EIkpjQuZ2C2pEJcXx8xqp0pY9U0ZaffCUzwmaInzBd9 fUBEXn0x/QR9fjhHUJP6EINxUePZRrxDyqNEKkeSKvoDshAL7BBGjze+Q2zGrrR3Bqo2 PHGrOR2smB25XcMKQLj78gALIdqYwVPUh4LzoM94W6PaW34dwvLOtJIXf+seJcaMZZGc 8F1U0BIOTkphW8eHBM2qyL2EPG+pGncWHo0AkNYVBETRzg9voz3Flx8UfUpQzTzJVNrY 42K7kdkrruqBugAsDIoxyTOmefCssUoVqWJrnNzbrN7wRRIWxMbZm38PN9pRCYsr9Hdy ckEg==
X-Gm-Message-State AD7BkJLjv6zVBMQ76kgY20hRbjbOJqO2ldf6pi9W0e9v5xMNEiBwKezu9DF/WqU7Cu5TSBQf5yWgKqABmdGpPw==
X-Received by 10.31.139.132 with SMTP id n126mr1433213vkd.78.1456498361966; Fri, 26 Feb 2016 06:52:41 -0800 (PST)
In-Reply-To <CAG5tnzrGFexYKaC6YhMaP-_vwudMgxwGNibh19DWmKLQMqp9vQ@mail.gmail.com>
X-Content-Filtered-By Mailman/MimeDel 2.1.21rc2
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21rc2
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>
Xref csiph.com comp.lang.python:103543

Show key headers only | View raw


Answer (obvious after a refreshing sleep):
Just run a separate async pool connection prior to kicking off the aiohttp
web application.
The startup now looks like:

async def connect():
    return await aiopg.create_pool(…)

if __name__ == “__main__”:
    loop = asyncio.get_event_loop()
    pool = loop.run_until_complete(connect())
    app = web.Application()
    app["pool"] = pool
    app.router.add_route(‘POST', '/pv/v1/', handle_v1)
    web.run_app(app)


Then, in the handle_v1 code:
    pool = request.app["pool"]
    connection = await pool.acquire()
    cursor = await connection.cursor()
—r

On Thu, Feb 25, 2016 at 5:23 PM, Ray Cote <rgacote@appropriatesolutions.com>
wrote:

> Hello:
>
> I have an aiohttp project that starts in the usual way:
>
> app = web.Application()
> app.router.add_route(‘POST”, ‘/‘, handler)
> web.run_app(app)
>
> My question is, how do I work with an aiopg.pool with aiohttp?
> There only seems to be async interfaces into aiopg — but I don’t want to
> create the pool in my handler since that requires a connection on each
> transaction.
>
> I found one example that was
>   app[“db”] = await aiopg.create_pool(dsn)
> but that doesn’t seen correct since we’re not yet in a loop.
>
> Can someone please provide an example showing the proper way to integrate
> aiopg pool into an aiohttp web application?
>
> Regards
> —Ray
>
> --
> Raymond Cote, President
> voice: +1.603.924.6079 email: rgacote@AppropriateSolutions.com skype:
> ray.cote
>
>
>


-- 
Raymond Cote, President
voice: +1.603.924.6079 email: rgacote@AppropriateSolutions.com skype:
ray.cote

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


Thread

Re: Looking for examples of using aiopg with aiohttp and non-async startup. Ray Cote <rgacote@appropriatesolutions.com> - 2016-02-26 09:52 -0500

csiph-web