@@ -502,6 +502,40 @@ The `create_async_connector` allows all the same input arguments as the
502502Once a ` Connector ` object is returned by ` create_async_connector ` you can call
503503its ` connect_async ` method, just as you would the ` connect ` method:
504504
505+ #### Asyncpg Connection Pool
506+
507+ ``` python
508+ import asyncpg
509+ from google.cloud.sql.connector import Connector, create_async_connector
510+
511+ async def main ():
512+ # initialize Connector object for connections to Cloud SQL
513+ connector = create_async_connector()
514+
515+ # creation function to generate asyncpg connections as the 'connect' arg
516+ async def getconn (instance_connection_name , ** kwargs ) -> asyncpg.Connection:
517+ return await connector.connect_async(
518+ instance_connection_name,
519+ " asyncpg" ,
520+ user = " my-user" ,
521+ password = " my-password" ,
522+ db = " my-db" ,
523+ ** kwargs, # ... additional asyncpg args
524+ )
525+
526+ # initialize connection pool
527+ pool = await asyncpg.create_pool(
528+ " my-project:my-region:my-instance" , connect = getconn
529+ )
530+
531+ # acquire connection and query Cloud SQL database
532+ async with pool.acquire() as conn:
533+ res = await conn.fetch(" SELECT NOW()" )
534+
535+ # close Connector
536+ await connector.close_async()
537+ ```
538+
505539#### SQLAlchemy Async Engine
506540
507541``` python
@@ -551,40 +585,6 @@ async def main():
551585 await pool.dispose()
552586```
553587
554- #### Asyncpg Connection Pool
555-
556- ``` python
557- import asyncpg
558- from google.cloud.sql.connector import Connector, create_async_connector
559-
560- async def main ():
561- # initialize Connector object for connections to Cloud SQL
562- connector = create_async_connector()
563-
564- # creation function to generate asyncpg connections as the 'connect' arg
565- async def getconn (instance_connection_name , ** kwargs ) -> asyncpg.Connection:
566- return await connector.connect_async(
567- instance_connection_name,
568- " asyncpg" ,
569- user = " my-user" ,
570- password = " my-password" ,
571- db = " my-db" ,
572- ** kwargs, # ... additional asyncpg args
573- )
574-
575- # initialize connection pool
576- pool = await asyncpg.create_pool(
577- " my-project:my-region:my-instance" , connect = getconn
578- )
579-
580- # acquire connection and query Cloud SQL database
581- async with pool.acquire() as conn:
582- res = await conn.fetch(" SELECT NOW()" )
583-
584- # close Connector
585- await connector.close_async()
586- ```
587-
588588For more details on additional database arguments with an ` asyncpg.Connection `
589589, please visit the
590590[ official documentation] ( https://magicstack.github.io/asyncpg/current/api/index.html ) .
@@ -600,6 +600,38 @@ calls to `connector.close_async()` to cleanup resources.
600600> This alternative requires that the running event loop be
601601> passed in as the ` loop ` argument to ` Connector() ` .
602602
603+ #### Asyncpg Connection Pool
604+
605+ ``` python
606+ import asyncpg
607+ from google.cloud.sql.connector import Connector, create_async_connector
608+
609+ async def main ():
610+ # initialize Connector object for connections to Cloud SQL
611+ loop = asyncio.get_running_loop()
612+ async with Connector(loop = loop) as connector:
613+
614+ # creation function to generate asyncpg connections as the 'connect' arg
615+ async def getconn (instance_connection_name , ** kwargs ) -> asyncpg.Connection:
616+ return await connector.connect_async(
617+ instance_connection_name,
618+ " asyncpg" ,
619+ user = " my-user" ,
620+ password = " my-password" ,
621+ db = " my-db" ,
622+ ** kwargs, # ... additional asyncpg args
623+ )
624+
625+ # create connection pool
626+ pool = await asyncpg.create_pool(
627+ " my-project:my-region:my-instance" , connect = getconn
628+ )
629+
630+ # acquire connection and query Cloud SQL database
631+ async with pool.acquire() as conn:
632+ res = await conn.fetch(" SELECT NOW()" )
633+ ```
634+
603635#### SQLAlchemy Async Engine
604636
605637``` python
@@ -647,38 +679,6 @@ async def main():
647679 await pool.dispose()
648680```
649681
650- #### Asyncpg Connection Pool
651-
652- ``` python
653- import asyncpg
654- from google.cloud.sql.connector import Connector, create_async_connector
655-
656- async def main ():
657- # initialize Connector object for connections to Cloud SQL
658- loop = asyncio.get_running_loop()
659- async with Connector(loop = loop) as connector:
660-
661- # creation function to generate asyncpg connections as the 'connect' arg
662- async def getconn (instance_connection_name , ** kwargs ) -> asyncpg.Connection:
663- return await connector.connect_async(
664- instance_connection_name,
665- " asyncpg" ,
666- user = " my-user" ,
667- password = " my-password" ,
668- db = " my-db" ,
669- ** kwargs, # ... additional asyncpg args
670- )
671-
672- # create connection pool
673- pool = await asyncpg.create_pool(
674- " my-project:my-region:my-instance" , connect = getconn
675- )
676-
677- # acquire connection and query Cloud SQL database
678- async with pool.acquire() as conn:
679- res = await conn.fetch(" SELECT NOW()" )
680- ```
681-
682682### Debug Logging
683683
684684The Cloud SQL Python Connector uses the standard [ Python logging module] [ python-logging ]
0 commit comments