Skip to content

Commit

Permalink
call in thread use pool
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-x-c committed Sep 10, 2024
1 parent 2ae6cdb commit 3ee5876
Showing 1 changed file with 3 additions and 15 deletions.
18 changes: 3 additions & 15 deletions src/agentscope/rpc/rpc_client.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# -*- coding: utf-8 -*-
""" Client of rpc agent server """

import threading
import json
import os
from typing import Optional, Sequence, Union, Generator, Callable, Any
from concurrent.futures import Future
from concurrent.futures import Future, ThreadPoolExecutor
from loguru import logger

from ..message import Msg
Expand Down Expand Up @@ -35,6 +34,7 @@ class RpcClient:
"""A client of Rpc agent server"""

_CHANNEL_POOL = {}
_EXECUTOR = ThreadPoolExecutor()

def __init__(
self,
Expand Down Expand Up @@ -336,19 +336,7 @@ def call_func_in_thread(func: Callable) -> Future:
Returns:
`Future`: A stub to get the response.
"""
future = Future()

def wrapper() -> None:
try:
result = func()
future.set_result(result)
except Exception as e:
future.set_exception(e)

thread = threading.Thread(target=wrapper)
thread.start()

return future
return RpcClient._EXECUTOR.submit(func) # pylint: disable=W0212


class RpcAgentClient(RpcClient):
Expand Down

0 comments on commit 3ee5876

Please sign in to comment.