@@ -502,6 +502,8 @@ 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+ #### SQLAlchemy Async Engine
506+
505507``` python
506508import asyncpg
507509
@@ -511,7 +513,7 @@ from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine
511513from google.cloud.sql.connector import Connector, create_async_connector
512514
513515async def init_connection_pool (connector : Connector) -> AsyncEngine:
514- # initialize Connector object for connections to Cloud SQL
516+ # creation function to generate asyncpg connections as 'async_creator' arg
515517 async def getconn () -> asyncpg.Connection:
516518 conn: asyncpg.Connection = await connector.connect_async(
517519 " project:region:instance" , # Cloud SQL instance connection name
@@ -549,6 +551,40 @@ async def main():
549551 await pool.dispose()
550552```
551553
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+
552588For more details on additional database arguments with an ` asyncpg.Connection `
553589, please visit the
554590[ official documentation] ( https://magicstack.github.io/asyncpg/current/api/index.html ) .
@@ -564,6 +600,8 @@ calls to `connector.close_async()` to cleanup resources.
564600> This alternative requires that the running event loop be
565601> passed in as the ` loop ` argument to ` Connector() ` .
566602
603+ #### SQLAlchemy Async Engine
604+
567605``` python
568606import asyncio
569607import asyncpg
@@ -574,17 +612,17 @@ from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine
574612from google.cloud.sql.connector import Connector
575613
576614async def init_connection_pool (connector : Connector) -> AsyncEngine:
577- # initialize Connector object for connections to Cloud SQL
615+ # creation function to generate asyncpg connections as 'async_creator' arg
578616 async def getconn () -> asyncpg.Connection:
579- conn: asyncpg.Connection = await connector.connect_async(
580- " project:region:instance" , # Cloud SQL instance connection name
581- " asyncpg" ,
582- user = " my-user" ,
583- password = " my-password" ,
584- db = " my-db-name"
585- # ... additional database driver args
586- )
587- return conn
617+ conn: asyncpg.Connection = await connector.connect_async(
618+ " project:region:instance" , # Cloud SQL instance connection name
619+ " asyncpg" ,
620+ user = " my-user" ,
621+ password = " my-password" ,
622+ db = " my-db-name"
623+ # ... additional database driver args
624+ )
625+ return conn
588626
589627 # The Cloud SQL Python Connector can be used along with SQLAlchemy using the
590628 # 'async_creator' argument to 'create_async_engine'
@@ -609,6 +647,38 @@ async def main():
609647 await pool.dispose()
610648```
611649
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+
612682### Debug Logging
613683
614684The Cloud SQL Python Connector uses the standard [ Python logging module] [ python-logging ]
0 commit comments