@@ -70,26 +70,6 @@ async def __start_session() -> None:
70
70
raise ValueError ("Session cannot be None." )
71
71
self .__async_client = ToolboxClient (url , self .__class__ .__session )
72
72
73
- def __run_as_sync (self , coro : Awaitable [T ]) -> T :
74
- """Run an async coroutine synchronously"""
75
- if not self .__loop :
76
- raise Exception (
77
- "Cannot call synchronous methods before the background loop is initialized."
78
- )
79
- return asyncio .run_coroutine_threadsafe (coro , self .__loop ).result ()
80
-
81
- async def __run_as_async (self , coro : Awaitable [T ]) -> T :
82
- """Run an async coroutine asynchronously"""
83
-
84
- # If a loop has not been provided, attempt to run in current thread.
85
- if not self .__loop :
86
- return await coro
87
-
88
- # Otherwise, run in the background thread.
89
- return await asyncio .wrap_future (
90
- asyncio .run_coroutine_threadsafe (coro , self .__loop )
91
- )
92
-
93
73
def close (self ):
94
74
"""
95
75
Synchronously closes the underlying client session. Doing so will cause
@@ -100,7 +80,7 @@ def close(self):
100
80
attempt to close it.
101
81
"""
102
82
coro = self .__session .close ()
103
- self . __run_as_sync (coro )
83
+ asyncio . run_coroutine_threadsafe (coro , self . __loop ). result ( )
104
84
105
85
def load_tool (
106
86
self ,
@@ -127,10 +107,8 @@ def load_tool(
127
107
for execution. The specific arguments and behavior of the callable
128
108
depend on the tool itself.
129
109
"""
130
-
131
- async_tool = self .__run_as_sync (
132
- self .__async_client .load_tool (name , auth_token_getters , bound_params )
133
- )
110
+ coro = self .__async_client .load_tool (name , auth_token_getters , bound_params )
111
+ async_tool = asyncio .run_coroutine_threadsafe (coro , self .__loop ).result ()
134
112
135
113
if not self .__loop or not self .__thread :
136
114
raise ValueError ("Background loop or thread cannot be None." )
@@ -156,10 +134,8 @@ def load_toolset(
156
134
list[ToolboxSyncTool]: A list of callables, one for each tool defined
157
135
in the toolset.
158
136
"""
159
-
160
- async_tools = self .__run_as_sync (
161
- self .__async_client .load_toolset (name , auth_token_getters , bound_params )
162
- )
137
+ coro = self .__async_client .load_toolset (name , auth_token_getters , bound_params )
138
+ async_tools = asyncio .run_coroutine_threadsafe (coro , self .__loop ).result ()
163
139
164
140
if not self .__loop or not self .__thread :
165
141
raise ValueError ("Background loop or thread cannot be None." )
0 commit comments