3434
3535class Client :
3636
37+ CALLBACK_THREADS_NUM = 5
38+
3739 def __init__ (self , client_configuration , topics , client_type : ClientType , tls_enable = False ):
3840 if client_configuration is None :
3941 raise IllegalArgumentException ("clientConfiguration should not be null." )
@@ -57,7 +59,7 @@ def __init__(self, client_configuration, topics, client_type: ClientType, tls_en
5759 else :
5860 self .__topics = set ()
5961 self .__callback_result_queue = Queue ()
60- self .__callback_result_thread = None
62+ self .__callback_threads = []
6163 self .__is_running = False
6264 self .__client_thread_task_enabled = False
6365 self .__had_shutdown = False
@@ -76,7 +78,7 @@ def startup(self):
7678 logger .warn (
7779 f"update topic exception when client startup, ignore it, try it again in scheduler. exception: { e } " )
7880 self .__start_scheduler ()
79- self .__start_callback_handler ()
81+ self .__start_async_rpc_callback_handler ()
8082 self .__is_running = True
8183 self ._start_success ()
8284 except Exception as e :
@@ -240,12 +242,19 @@ def __schedule_clear_idle_rpc_channels(self):
240242
241243 """ callback handler for async method """
242244
243- def __start_callback_handler (self ):
245+ def __start_async_rpc_callback_handler (self ):
244246 # a thread to handle callback when using async method such as send_async(), receive_async().
245247 # this handler switches user's callback thread from RpcClient's _io_loop_thread to client's callback_handler_thread
246- self .__callback_result_thread = threading .Thread (name = "callback_handler_thread" , target = self .__handle_callback )
247- self .__callback_result_thread .daemon = True
248- self .__callback_result_thread .start ()
248+ try :
249+ for i in range (Client .CALLBACK_THREADS_NUM ):
250+ th = threading .Thread (name = f"callback_handler_thread-{ i } " , target = self .__handle_callback )
251+ th .daemon = True
252+ self .__callback_threads .append (th )
253+ th .start ()
254+ logger .info (f"{ self .__str__ ()} start async rpc callback thread:{ th } success." )
255+ except Exception as e :
256+ print (f"{ self .__str__ ()} start async rpc callback raise exception: { e } " )
257+ raise e
249258
250259 def __handle_callback (self ):
251260 while True :
@@ -263,7 +272,7 @@ def __handle_callback(self):
263272 self .__callback_result_queue .task_done ()
264273 else :
265274 break
266- logger .info (f"{ self .__str__ ()} stop client callback result handler thread success." )
275+ logger .info (f"{ self .__str__ ()} stop client callback result handler thread: { threading . current_thread () } success." )
267276
268277 """ protect """
269278
@@ -375,6 +384,7 @@ def __setting_write(self, endpoints):
375384 req = self ._sync_setting_req (endpoints )
376385 callback = functools .partial (self .__setting_write_callback , endpoints = endpoints )
377386 future = self .__rpc_client .telemetry_write_async (endpoints , req )
387+ logger .debug (f"{ self .__str__ ()} send setting to { endpoints .__str__ ()} , { req } " )
378388 future .add_done_callback (callback )
379389
380390 def __retrieve_telemetry_stream_stream_call (self , endpoints , rebuild = False ):
@@ -466,9 +476,11 @@ def __stop_client_threads(self):
466476 self .__clear_idle_rpc_channels_threading_event .set ()
467477 self .__clear_idle_rpc_channels_scheduler .join ()
468478
469- if self . __callback_result_thread is not None :
479+ for i in range ( Client . CALLBACK_THREADS_NUM ) :
470480 self ._set_future_callback_result (CallbackResult .end_callback_thread_result ())
471- self .__callback_result_thread .join ()
481+
482+ for i in range (Client .CALLBACK_THREADS_NUM ):
483+ self .__callback_threads [i ].join ()
472484
473485 self .__topic_route_scheduler = None
474486 self .__topic_route_scheduler_threading_event = None
@@ -478,7 +490,8 @@ def __stop_client_threads(self):
478490 self .__sync_setting_scheduler_threading_event = None
479491 self .__clear_idle_rpc_channels_scheduler = None
480492 self .__clear_idle_rpc_channels_threading_event = None
481- self .__callback_result_thread = None
493+ self .__callback_result_queue = None
494+ self .__callback_threads = None
482495
483496 """ property """
484497
0 commit comments