@@ -34,10 +34,22 @@ Currently supported drivers are:
34
34
35
35
You can install this library with ` pip install ` :
36
36
37
+ ### pg8000
38
+
37
39
``` sh
38
40
pip install " google-cloud-alloydb-connector[pg8000]"
39
41
```
40
42
43
+ See [ Synchronous Driver Usage] ( #synchronous-driver-usage ) for details.
44
+
45
+ ### asyncpg
46
+
47
+ ``` sh
48
+ pip install " google-cloud-alloydb-connector[asyncpg]"
49
+ ```
50
+
51
+ See [ Async Driver Usage] ( #async-driver-usage ) for details.
52
+
41
53
### APIs and Services
42
54
43
55
This package requires the following to connect successfully:
@@ -70,7 +82,7 @@ This package provides several functions for authorizing and encrypting
70
82
connections. These functions are used with your database driver to connect to
71
83
your AlloyDB instance.
72
84
73
- AlloyDB supports network connectivity through private, internal IP addresses only.
85
+ AlloyDB supports network connectivity through private, internal IP addresses only.
74
86
This package must be run in an environment that is connected to the
75
87
[ VPC Network] [ vpc ] that hosts your AlloyDB private IP address.
76
88
@@ -79,7 +91,7 @@ Please see [Configuring AlloyDB Connectivity][alloydb-connectivity] for more det
79
91
[ vpc ] : https://cloud.google.com/vpc/docs/vpc
80
92
[ alloydb-connectivity ] : https://cloud.google.com/alloydb/docs/configure-connectivity
81
93
82
- ### How to use this Connector
94
+ ### Synchronous Driver Usage
83
95
84
96
To connect to AlloyDB using the connector, inititalize a ` Connector `
85
97
object and call it's ` connect ` method with the proper input parameters.
@@ -151,7 +163,7 @@ To close the `Connector` object's background resources, call it's `close()` meth
151
163
connector.close()
152
164
```
153
165
154
- ### Using Connector as a Context Manager
166
+ ### Synchronous Context Manager
155
167
156
168
The ` Connector ` object can also be used as a context manager in order to
157
169
automatically close and cleanup resources, removing the need for explicit
@@ -202,6 +214,118 @@ with pool.connect() as db_conn:
202
214
print (row)
203
215
```
204
216
217
+ ### Async Driver Usage
218
+
219
+ The AlloyDB Connector is compatible with [ asyncio] [ ] to improve the speed and
220
+ efficiency of database connections through concurrency. The ` AsyncConnector `
221
+ currently supports the following asyncio database drivers:
222
+
223
+ - [ asyncpg] ( https://magicstack.github.io/asyncpg )
224
+
225
+ [ asyncio ] : https://docs.python.org/3/library/asyncio.html
226
+
227
+ ``` python
228
+ import asyncpg
229
+
230
+ import sqlalchemy
231
+ from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine
232
+
233
+ from google.cloud.alloydb.connector import AsyncConnector
234
+
235
+ async def init_connection_pool (connector : AsyncConnector) -> AsyncEngine:
236
+ # initialize Connector object for connections to AlloyDB
237
+ async def getconn () -> asyncpg.Connection:
238
+ conn: asyncpg.Connection = await connector.connect(
239
+ " projects/<YOUR_PROJECT>/locations/<YOUR_REGION>/clusters/<YOUR_CLUSTER>/instances/<YOUR_INSTANCE>" ,
240
+ " asyncpg" ,
241
+ user = " my-user" ,
242
+ password = " my-password" ,
243
+ db = " my-db-name"
244
+ # ... additional database driver args
245
+ )
246
+ return conn
247
+
248
+ # The AlloyDB Python Connector can be used along with SQLAlchemy using the
249
+ # 'async_creator' argument to 'create_async_engine'
250
+ pool = create_async_engine(
251
+ " postgresql+asyncpg://" ,
252
+ async_creator = getconn,
253
+ )
254
+ return pool
255
+
256
+ async def main ():
257
+ connector = AsyncConnector()
258
+
259
+ # initialize connection pool
260
+ pool = await init_connection_pool(connector)
261
+
262
+ # example query
263
+ async with pool.connect() as conn:
264
+ await conn.execute(sqlalchemy.text(" SELECT NOW()" ))
265
+
266
+ # dispose of connection pool
267
+ await pool.dispose()
268
+
269
+ # close Connector
270
+ await connector.close()
271
+
272
+ ```
273
+
274
+ For more details on additional arguments with an ` asyncpg.Connection ` , please
275
+ visit the [ official documentation] [ asyncpg-docs ] .
276
+
277
+
278
+ [ asyncpg-docs ] : https://magicstack.github.io/asyncpg/current/api/index.html
279
+
280
+ ### Async Context Manager
281
+
282
+ The ` AsyncConnector ` also may be used as an async context manager, removing the
283
+ need for explicit calls to ` connector.close() ` to cleanup resources.
284
+
285
+ ``` python
286
+ import asyncio
287
+ import asyncpg
288
+
289
+ import sqlalchemy
290
+ from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine
291
+
292
+ from google.cloud.alloydb.connector import AsyncConnector
293
+
294
+ async def init_connection_pool (connector : AsyncConnector) -> AsyncEngine:
295
+ # initialize Connector object for connections to AlloyDB
296
+ async def getconn () -> asyncpg.Connection:
297
+ conn: asyncpg.Connection = await connector.connect(
298
+ " projects/<YOUR_PROJECT>/locations/<YOUR_REGION>/clusters/<YOUR_CLUSTER>/instances/<YOUR_INSTANCE>" ,
299
+ " asyncpg" ,
300
+ user = " my-user" ,
301
+ password = " my-password" ,
302
+ db = " my-db-name"
303
+ # ... additional database driver args
304
+ )
305
+ return conn
306
+
307
+ # The AlloyDB Python Connector can be used along with SQLAlchemy using the
308
+ # 'async_creator' argument to 'create_async_engine'
309
+ pool = create_async_engine(
310
+ " postgresql+asyncpg://" ,
311
+ async_creator = getconn,
312
+ )
313
+ return pool
314
+
315
+ async def main ():
316
+ # initialize Connector object for connections to AlloyDB
317
+ async with AsyncConnector() as connector:
318
+ # initialize connection pool
319
+ pool = await init_connection_pool(connector)
320
+
321
+ # example query
322
+ async with pool.connect() as conn:
323
+ await conn.execute(sqlalchemy.text(" SELECT NOW()" ))
324
+
325
+ # dispose of connection pool
326
+ await pool.dispose()
327
+ ```
328
+
205
329
## Support policy
206
330
207
331
### Major version lifecycle
0 commit comments