Skip to content

Commit 033c85d

Browse files
committed
remove run_as_sync and run_as_async methods
1 parent 68c82d2 commit 033c85d

File tree

2 files changed

+7
-50
lines changed

2 files changed

+7
-50
lines changed

packages/toolbox-core/src/toolbox_core/sync_client.py

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -70,26 +70,6 @@ async def __start_session() -> None:
7070
raise ValueError("Session cannot be None.")
7171
self.__async_client = ToolboxClient(url, self.__class__.__session)
7272

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-
9373
def close(self):
9474
"""
9575
Synchronously closes the underlying client session. Doing so will cause
@@ -100,7 +80,7 @@ def close(self):
10080
attempt to close it.
10181
"""
10282
coro = self.__session.close()
103-
self.__run_as_sync(coro)
83+
asyncio.run_coroutine_threadsafe(coro, self.__loop).result()
10484

10585
def load_tool(
10686
self,
@@ -127,10 +107,8 @@ def load_tool(
127107
for execution. The specific arguments and behavior of the callable
128108
depend on the tool itself.
129109
"""
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()
134112

135113
if not self.__loop or not self.__thread:
136114
raise ValueError("Background loop or thread cannot be None.")
@@ -156,10 +134,8 @@ def load_toolset(
156134
list[ToolboxSyncTool]: A list of callables, one for each tool defined
157135
in the toolset.
158136
"""
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()
163139

164140
if not self.__loop or not self.__thread:
165141
raise ValueError("Background loop or thread cannot be None.")

packages/toolbox-core/src/toolbox_core/sync_tool.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,26 +62,6 @@ def __init__(
6262
self.__annotations__ = self.__async_tool.__annotations__
6363
# TODO: self.__qualname__ ?? (Consider if needed)
6464

65-
def __run_as_sync(self, coro: Awaitable[T]) -> T:
66-
"""Run an async coroutine synchronously"""
67-
if not self.__loop:
68-
raise Exception(
69-
"Cannot call synchronous methods before the background loop is initialized."
70-
)
71-
return asyncio.run_coroutine_threadsafe(coro, self.__loop).result()
72-
73-
async def __run_as_async(self, coro: Awaitable[T]) -> T:
74-
"""Run an async coroutine asynchronously"""
75-
76-
# If a loop has not been provided, attempt to run in current thread.
77-
if not self.__loop:
78-
return await coro
79-
80-
# Otherwise, run in the background thread.
81-
return await asyncio.wrap_future(
82-
asyncio.run_coroutine_threadsafe(coro, self.__loop)
83-
)
84-
8565
def __call__(self, *args: Any, **kwargs: Any) -> str:
8666
"""
8767
Synchronously calls the remote tool with the provided arguments.
@@ -96,7 +76,8 @@ def __call__(self, *args: Any, **kwargs: Any) -> str:
9676
Returns:
9777
The string result returned by the remote tool execution.
9878
"""
99-
return self.__run_as_sync(self.__async_tool(*args, **kwargs))
79+
coro = self.__async_tool(*args, **kwargs)
80+
return asyncio.run_coroutine_threadsafe(coro, self.__loop).result()
10081

10182
def add_auth_token_getters(
10283
self,

0 commit comments

Comments
 (0)