@@ -430,48 +430,55 @@ Once a `Connector` object is returned by `create_async_connector` you can call
430430its ` connect_async ` method, just as you would the ` connect ` method:
431431
432432``` python
433- import asyncio
434433import asyncpg
435- from google.cloud.sql.connector import create_async_connector
436434
435+ import sqlalchemy
436+ from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine
437437
438- async def main ():
439- # intialize Connector object using 'create_async_connector'
440- connector = await create_async_connector()
438+ from google.cloud.sql.connector import Connector, create_async_connector
441439
442- # create connection to Cloud SQL database
443- conn: asyncpg.Connection = await connector.connect_async(
444- " project:region:instance" , # Cloud SQL instance connection name
445- " asyncpg" ,
446- user = " my-user" ,
447- password = " my-password" ,
448- db = " my-db-name"
449- # ... additional database driver args
450- )
440+ async def init_connection_pool (connector : Connector) -> AsyncEngine:
441+ # initialize Connector object for connections to Cloud SQL
442+ async def getconn () -> asyncpg.Connection:
443+ conn: asyncpg.Connection = await connector.connect_async(
444+ " project:region:instance" , # Cloud SQL instance connection name
445+ " asyncpg" ,
446+ user = " my-user" ,
447+ password = " my-password" ,
448+ db = " my-db-name"
449+ # ... additional database driver args
450+ )
451+ return conn
451452
452- # insert into Cloud SQL database (example)
453- await conn.execute(" INSERT INTO ratings (title, genre, rating) VALUES ('Batman', 'Action', 8.2)" )
453+ # The Cloud SQL Python Connector can be used along with SQLAlchemy using the
454+ # 'async_creator' argument to 'create_async_engine'
455+ pool = create_async_engine(
456+ " postgresql+asyncpg://" ,
457+ async_creator = getconn,
458+ )
459+ return pool
454460
455- # query Cloud SQL database (example)
456- results = await conn.fetch(" SELECT * from ratings" )
461+ async def main ():
462+ # initialize Connector object for connections to Cloud SQL
463+ connector = await create_async_connector()
457464
458- # ... do something with results
459- for row in results:
460- print (row)
465+ # initialize connection pool
466+ pool = await init_connection_pool(connector)
461467
462- # close asyncpg connection
463- await conn.close()
468+ # example query
469+ async with pool.connect() as conn:
470+ await conn.execute(sqlalchemy.text(" SELECT NOW()" ))
464471
465- # close Cloud SQL Connector
472+ # close Connector
466473 await connector.close_async()
467474
468-
469- # Test connection with `asyncio`
470- asyncio.run(main())
475+ # dispose of connection pool
476+ await pool.dispose()
471477```
472478
473- For more details on interacting with an ` asyncpg.Connection ` , please visit
474- the [ official documentation] ( https://magicstack.github.io/asyncpg/current/api/index.html ) .
479+ For more details on additional database arguments with an ` asyncpg.Connection `
480+ , please visit the
481+ [ official documentation] ( https://magicstack.github.io/asyncpg/current/api/index.html ) .
475482
476483### Async Context Manager
477484
@@ -485,44 +492,52 @@ passed in as the `loop` argument to `Connector()`.
485492``` python
486493import asyncio
487494import asyncpg
495+
496+ import sqlalchemy
497+ from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine
498+
488499from google.cloud.sql.connector import Connector
489500
501+ async def init_connection_pool (connector : Connector) -> AsyncEngine:
502+ # initialize Connector object for connections to Cloud SQL
503+ async def getconn () -> asyncpg.Connection:
504+ conn: asyncpg.Connection = await connector.connect_async(
505+ " project:region:instance" , # Cloud SQL instance connection name
506+ " asyncpg" ,
507+ user = " my-user" ,
508+ password = " my-password" ,
509+ db = " my-db-name"
510+ # ... additional database driver args
511+ )
512+ return conn
513+
514+ # The Cloud SQL Python Connector can be used along with SQLAlchemy using the
515+ # 'async_creator' argument to 'create_async_engine'
516+ pool = create_async_engine(
517+ " postgresql+asyncpg://" ,
518+ async_creator = getconn,
519+ )
520+ return pool
521+
490522async def main ():
491- # get current running event loop to be used with Connector
523+ # initialize Connector object for connections to Cloud SQL
492524 loop = asyncio.get_running_loop()
493- # intialize Connector object as async context manager
494525 async with Connector(loop = loop) as connector:
526+ # initialize connection pool
527+ pool = await init_connection_pool(connector)
495528
496- # create connection to Cloud SQL database
497- conn: asyncpg.Connection = await connector.connect_async(
498- " project:region:instance" , # Cloud SQL instance connection name
499- " asyncpg" ,
500- user = " my-user" ,
501- password = " my-password" ,
502- db = " my-db-name"
503- # ... additional database driver args
504- )
505-
506- # insert into Cloud SQL database (example)
507- await conn.execute(" INSERT INTO ratings (title, genre, rating) VALUES ('Batman', 'Action', 8.2)" )
508-
509- # query Cloud SQL database (example)
510- results = await conn.fetch(" SELECT * from ratings" )
511-
512- # ... do something with results
513- for row in results:
514- print (row)
529+ # example query
530+ async with pool.connect() as conn:
531+ await conn.execute(sqlalchemy.text(" SELECT NOW()" ))
515532
516- # close asyncpg connection
517- await conn.close()
518-
519- # Test connection with `asyncio`
520- asyncio.run(main())
533+ # dispose of connection pool
534+ await pool.dispose()
521535```
522536
523537## Support policy
524538
525539### Major version lifecycle
540+
526541This project uses [ semantic versioning] ( https://semver.org/ ) , and uses the
527542following lifecycle regarding support for a major version:
528543
0 commit comments