3232 runtime_checkable ,
3333)
3434
35- from robotcode .core .async_tools import create_sub_task , run_coroutine_in_thread
36- from robotcode .core .concurrent import (
37- FutureEx ,
38- is_threaded_callable ,
39- run_in_thread ,
40- )
35+ from robotcode .core .async_tools import run_coroutine_in_thread
36+ from robotcode .core .concurrent import FutureEx , run_in_thread
4137from robotcode .core .event import event
4238from robotcode .core .utils .dataclasses import as_json , from_dict
4339from robotcode .core .utils .inspect import ensure_coroutine , iter_methods
@@ -150,6 +146,7 @@ class RpcMethodEntry:
150146 method : Callable [..., Any ]
151147 param_type : Optional [Type [Any ]]
152148 cancelable : bool
149+ threaded : bool
153150
154151 _is_coroutine : Optional [bool ] = field (default = None , init = False )
155152
@@ -181,6 +178,7 @@ def rpc_method(
181178 name : Optional [str ] = None ,
182179 param_type : Optional [Type [Any ]] = None ,
183180 cancelable : bool = True ,
181+ threaded : bool = False ,
184182) -> Callable [[_F ], _F ]:
185183 ...
186184
@@ -191,6 +189,7 @@ def rpc_method(
191189 name : Optional [str ] = None ,
192190 param_type : Optional [Type [Any ]] = None ,
193191 cancelable : bool = True ,
192+ threaded : bool = False ,
194193) -> Callable [[_F ], _F ]:
195194 def _decorator (func : _F ) -> Callable [[_F ], _F ]:
196195 if inspect .isclass (_func ):
@@ -207,7 +206,7 @@ def _decorator(func: _F) -> Callable[[_F], _F]:
207206 if real_name is None or not real_name :
208207 raise ValueError ("name is empty." )
209208
210- cast (RpcMethod , f ).__rpc_method__ = RpcMethodEntry (real_name , f , param_type , cancelable )
209+ cast (RpcMethod , f ).__rpc_method__ = RpcMethodEntry (real_name , f , param_type , cancelable , threaded )
211210 return func
212211
213212 if _func is None :
@@ -274,6 +273,7 @@ def get_methods(obj: Any) -> Dict[str, RpcMethodEntry]:
274273 method ,
275274 rpc_method .__rpc_method__ .param_type ,
276275 rpc_method .__rpc_method__ .cancelable ,
276+ rpc_method .__rpc_method__ .threaded ,
277277 )
278278 for method , rpc_method in map (
279279 lambda m1 : (m1 , cast (RpcMethod , m1 )),
@@ -324,10 +324,11 @@ def add_method(
324324 func : Callable [..., Any ],
325325 param_type : Optional [Type [Any ]] = None ,
326326 cancelable : bool = True ,
327+ threaded : bool = False ,
327328 ) -> None :
328329 self .__ensure_initialized ()
329330
330- self .__methods [name ] = RpcMethodEntry (name , func , param_type , cancelable )
331+ self .__methods [name ] = RpcMethodEntry (name , func , param_type , cancelable , threaded )
331332
332333 def remove_method (self , name : str ) -> Optional [RpcMethodEntry ]:
333334 self .__ensure_initialized ()
@@ -741,12 +742,10 @@ async def handle_request(self, message: JsonRPCRequest) -> None:
741742
742743 params = self ._convert_params (e .method , e .param_type , message .params )
743744
744- is_threaded_method = is_threaded_callable (e .method )
745-
746- if not is_threaded_method and not e .is_coroutine :
745+ if not e .threaded and not e .is_coroutine :
747746 self .send_response (message .id , e .method (* params [0 ], ** params [1 ]))
748747 else :
749- if is_threaded_method :
748+ if e . threaded :
750749 if e .is_coroutine :
751750 task = run_coroutine_in_thread (
752751 ensure_coroutine (cast (Callable [..., Any ], e .method )),
@@ -756,10 +755,7 @@ async def handle_request(self, message: JsonRPCRequest) -> None:
756755 else :
757756 task = asyncio .wrap_future (run_in_thread (e .method , * params [0 ], ** params [1 ]))
758757 else :
759- task = create_sub_task (
760- ensure_coroutine (e .method )(* params [0 ], ** params [1 ]),
761- name = message .method ,
762- )
758+ task = asyncio .create_task (e .method (* params [0 ], ** params [1 ]), name = message .method )
763759
764760 with self ._received_request_lock :
765761 self ._received_request [message .id ] = ReceivedRequestEntry (task , message , e .cancelable )
@@ -845,20 +841,18 @@ async def handle_notification(self, message: JsonRPCNotification) -> None:
845841 try :
846842 params = self ._convert_params (e .method , e .param_type , message .params )
847843
848- if not e .is_coroutine :
844+ if not e .threaded and not e . is_coroutine :
849845 e .method (* params [0 ], ** params [1 ])
850846 else :
851- if is_threaded_callable (e .method ):
852- task = run_coroutine_in_thread (
853- ensure_coroutine (cast (Callable [..., Any ], e .method )),
854- * params [0 ],
855- ** params [1 ],
856- )
847+ if e .threaded :
848+ if e .is_coroutine :
849+ task = run_coroutine_in_thread (
850+ ensure_coroutine (cast (Callable [..., Any ], e .method )), * params [0 ], ** params [1 ]
851+ )
852+ else :
853+ task = asyncio .wrap_future (run_in_thread (e .method , * params [0 ], ** params [1 ]))
857854 else :
858- task = create_sub_task (
859- ensure_coroutine (e .method )(* params [0 ], ** params [1 ]),
860- name = message .method ,
861- )
855+ task = asyncio .create_task (e .method (* params [0 ], ** params [1 ]), name = message .method )
862856
863857 await task
864858 except asyncio .CancelledError :
0 commit comments