@@ -107,6 +107,8 @@ class PostgresEngine:
107107 """A class for managing connections to a Cloud SQL for Postgres database."""
108108
109109 _connector : Optional [Connector ] = None
110+ _default_loop : Optional [asyncio .AbstractEventLoop ] = None
111+ _default_thread : Optional [Thread ] = None
110112 __create_key = object ()
111113
112114 def __init__ (
@@ -166,9 +168,12 @@ def from_instance(
166168 """
167169 # Running a loop in a background thread allows us to support
168170 # async methods from non-async environments
169- loop = asyncio .new_event_loop ()
170- thread = Thread (target = loop .run_forever , daemon = True )
171- thread .start ()
171+ if cls ._default_loop is None :
172+ cls ._default_loop = asyncio .new_event_loop ()
173+ cls ._default_thread = Thread (
174+ target = cls ._default_loop .run_forever , daemon = True
175+ )
176+ cls ._default_thread .start ()
172177 coro = cls ._create (
173178 project_id ,
174179 region ,
@@ -177,12 +182,12 @@ def from_instance(
177182 ip_type ,
178183 user ,
179184 password ,
180- loop = loop ,
181- thread = thread ,
185+ loop = cls . _default_loop ,
186+ thread = cls . _default_thread ,
182187 quota_project = quota_project ,
183188 iam_account_email = iam_account_email ,
184189 )
185- return asyncio .run_coroutine_threadsafe (coro , loop ).result ()
190+ return asyncio .run_coroutine_threadsafe (coro , cls . _default_loop ).result ()
186191
187192 @classmethod
188193 async def _create (
@@ -228,7 +233,7 @@ async def _create(
228233 )
229234 if cls ._connector is None :
230235 cls ._connector = Connector (
231- loop = asyncio . get_event_loop () ,
236+ loop = loop ,
232237 user_agent = USER_AGENT ,
233238 quota_project = quota_project ,
234239 refresh_strategy = RefreshStrategy .LAZY ,
0 commit comments