From 3ee58760e46a8997fa29a70bb1bec53d98894a28 Mon Sep 17 00:00:00 2001 From: "panxuchen.pxc" Date: Tue, 10 Sep 2024 19:12:08 +0800 Subject: [PATCH] call in thread use pool --- src/agentscope/rpc/rpc_client.py | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/src/agentscope/rpc/rpc_client.py b/src/agentscope/rpc/rpc_client.py index 0f3d47c5a..bc5215797 100644 --- a/src/agentscope/rpc/rpc_client.py +++ b/src/agentscope/rpc/rpc_client.py @@ -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 @@ -35,6 +34,7 @@ class RpcClient: """A client of Rpc agent server""" _CHANNEL_POOL = {} + _EXECUTOR = ThreadPoolExecutor() def __init__( self, @@ -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):