|
32 | 32 | from google.cloud.sql.connector.enums import DriverMapping |
33 | 33 | from google.cloud.sql.connector.enums import IPTypes |
34 | 34 | from google.cloud.sql.connector.enums import RefreshStrategy |
35 | | -from google.cloud.sql.connector.exceptions import ConnectorLoopError |
36 | 35 | from google.cloud.sql.connector.instance import RefreshAheadCache |
37 | 36 | from google.cloud.sql.connector.lazy import LazyRefreshCache |
38 | 37 | import google.cloud.sql.connector.pg8000 as pg8000 |
@@ -208,27 +207,16 @@ def connect( |
208 | 207 |
|
209 | 208 | Returns: |
210 | 209 | A DB-API connection to the specified Cloud SQL instance. |
211 | | -
|
212 | | - Raises: |
213 | | - ConnectorLoopError: Event loop for background refresh is running in |
214 | | - current thread. Error instead of hanging indefinitely. |
215 | 210 | """ |
216 | | - try: |
217 | | - # check if event loop is running in current thread |
218 | | - if self._loop == asyncio.get_running_loop(): |
219 | | - raise ConnectorLoopError( |
220 | | - "Connector event loop is running in current thread!" |
221 | | - "Event loop must be attached to a different thread to prevent blocking code!" |
222 | | - ) |
223 | | - # asyncio.get_running_loop will throw RunTimeError if no running loop is present |
224 | | - except RuntimeError: |
225 | | - pass |
226 | 211 |
|
227 | | - # if event loop is not in current thread, proceed with connection |
228 | | - connect_task = asyncio.run_coroutine_threadsafe( |
229 | | - self.connect_async(instance_connection_string, driver, **kwargs), self._loop |
| 212 | + # connect runs sync database connections on background thread. |
| 213 | + # Async database connections should call 'connect_async' directly to |
| 214 | + # avoid hanging indefinitely. |
| 215 | + connect_future = asyncio.run_coroutine_threadsafe( |
| 216 | + self.connect_async(instance_connection_string, driver, **kwargs), |
| 217 | + self._loop, |
230 | 218 | ) |
231 | | - return connect_task.result() |
| 219 | + return connect_future.result() |
232 | 220 |
|
233 | 221 | async def connect_async( |
234 | 222 | self, instance_connection_string: str, driver: str, **kwargs: Any |
|
0 commit comments