Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ray Cote 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: References: 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: 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: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:103543 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(=E2=80=A6) if __name__ =3D=3D =E2=80=9C__main__=E2=80=9D: loop =3D asyncio.get_event_loop() pool =3D loop.run_until_complete(connect()) app =3D web.Application() app["pool"] =3D pool app.router.add_route(=E2=80=98POST', '/pv/v1/', handle_v1) web.run_app(app) Then, in the handle_v1 code: pool =3D request.app["pool"] connection =3D await pool.acquire() cursor =3D await connection.cursor() =E2=80=94r On Thu, Feb 25, 2016 at 5:23 PM, Ray Cote wrote: > Hello: > > I have an aiohttp project that starts in the usual way: > > app =3D web.Application() > app.router.add_route(=E2=80=98POST=E2=80=9D, =E2=80=98/=E2=80=98, 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 =E2=80=94 but I don=E2= =80=99t want to > create the pool in my handler since that requires a connection on each > transaction. > > I found one example that was > app[=E2=80=9Cdb=E2=80=9D] =3D await aiopg.create_pool(dsn) > but that doesn=E2=80=99t seen correct since we=E2=80=99re not yet in a lo= op. > > Can someone please provide an example showing the proper way to integrate > aiopg pool into an aiohttp web application? > > Regards > =E2=80=94Ray > > -- > Raymond Cote, President > voice: +1.603.924.6079 email: rgacote@AppropriateSolutions.com skype: > ray.cote > > > --=20 Raymond Cote, President voice: +1.603.924.6079 email: rgacote@AppropriateSolutions.com skype: ray.cote