File tree Expand file tree Collapse file tree 2 files changed +14
-1
lines changed
google/cloud/alloydb/connector Expand file tree Collapse file tree 2 files changed +14
-1
lines changed Original file line number Diff line number Diff line change @@ -63,7 +63,13 @@ def __init__(
63
63
# otherwise use application default credentials
64
64
else :
65
65
self ._credentials , _ = default (scopes = scopes )
66
- self ._keys = asyncio .create_task (generate_keys ())
66
+
67
+ # check if AsyncConnector is being initialized with event loop running
68
+ # Otherwise we will lazy init keys
69
+ try :
70
+ self ._keys = asyncio .create_task (generate_keys ())
71
+ except RuntimeError :
72
+ self ._keys = None
67
73
self ._client : Optional [AlloyDBClient ] = None
68
74
69
75
async def connect (
@@ -90,6 +96,8 @@ async def connect(
90
96
Returns:
91
97
connection: A DBAPI connection to the specified AlloyDB instance.
92
98
"""
99
+ if self ._keys is None :
100
+ self ._keys = asyncio .create_task (generate_keys )
93
101
if self ._client is None :
94
102
# lazy init client as it has to be initialized in async context
95
103
self ._client = AlloyDBClient (
Original file line number Diff line number Diff line change @@ -191,3 +191,8 @@ async def test_connect_unsupported_driver(
191
191
exc_info .value .args [0 ]
192
192
== "Driver 'bad_driver' is not a supported database driver."
193
193
)
194
+
195
+
196
+ def test_synchronous_init (credentials : FakeCredentials ) -> None :
197
+ connector = AsyncConnector (credentials )
198
+ assert connector ._keys is None
You can’t perform that action at this time.
0 commit comments