1
1
from __future__ import annotations
2
2
3
3
import asyncio
4
+ import concurrent .futures
4
5
import inspect
5
6
import json
6
7
import re
7
8
import threading
8
- import time
9
9
import weakref
10
10
from abc import ABC , abstractmethod
11
11
from collections import OrderedDict
36
36
from robotcode .core .async_tools import (
37
37
HasThreaded ,
38
38
async_event ,
39
- create_sub_future ,
40
39
create_sub_task ,
41
40
run_coroutine_in_thread ,
42
41
)
@@ -336,7 +335,7 @@ def get_param_type(self, name: str) -> Optional[Type[Any]]:
336
335
337
336
338
337
class SendedRequestEntry (NamedTuple ):
339
- future : asyncio .Future [Any ]
338
+ future : concurrent . futures .Future [Any ]
340
339
result_type : Optional [Type [Any ]]
341
340
342
341
@@ -546,9 +545,9 @@ def send_request(
546
545
method : str ,
547
546
params : Optional [Any ] = None ,
548
547
return_type_or_converter : Optional [Type [_TResult ]] = None ,
549
- ) -> asyncio . Future [Optional [ _TResult ] ]:
548
+ ) -> concurrent . futures . Future [_TResult ]:
550
549
with self ._sended_request_lock :
551
- result : asyncio . Future [Optional [ _TResult ]] = create_sub_future ()
550
+ result : concurrent . futures . Future [_TResult ] = concurrent . futures . Future ()
552
551
self ._sended_request_count += 1
553
552
id = self ._sended_request_count
554
553
@@ -559,13 +558,13 @@ def send_request(
559
558
560
559
return result
561
560
562
- async def send_request_async (
561
+ def send_request_async (
563
562
self ,
564
563
method : str ,
565
564
params : Optional [Any ] = None ,
566
565
return_type : Optional [Type [_TResult ]] = None ,
567
- ) -> Optional [_TResult ]:
568
- return await self .send_request (method , params , return_type )
566
+ ) -> asyncio . Future [_TResult ]:
567
+ return asyncio . wrap_future ( self .send_request (method , params , return_type ) )
569
568
570
569
@__logger .call
571
570
def send_notification (self , method : str , params : Any ) -> None :
@@ -590,44 +589,17 @@ async def handle_response(self, message: JsonRPCResponse) -> None:
590
589
591
590
try :
592
591
if not entry .future .done ():
593
- res = None
594
- if message .result is not None :
595
- res = from_dict (message .result , entry .result_type )
596
- if entry .future .get_loop () == asyncio .get_running_loop ():
597
- entry .future .set_result (res )
598
- else :
599
- if entry .future .get_loop ().is_running ():
600
-
601
- def set_result (f : asyncio .Future [Any ], r : Any , ev : threading .Event ) -> None :
602
- try :
603
- if not f .done () and f .get_loop ().is_running ():
604
- f .set_result (r )
605
- finally :
606
- ev .set ()
607
-
608
- done = threading .Event ()
609
-
610
- entry .future .get_loop ().call_soon_threadsafe (set_result , entry .future , res , done )
611
-
612
- start = time .monotonic ()
613
- while not done .is_set ():
614
- if time .monotonic () - start > 120 :
615
- raise TimeoutError ("Can't set future result." )
616
-
617
- await asyncio .sleep (0 )
618
-
619
- else :
620
- self .__logger .warning (lambda : f"Response { entry !r} loop is not running." )
592
+ entry .future .set_result (
593
+ from_dict (message .result , entry .result_type ) if message .result is not None else None
594
+ )
595
+ else :
596
+ self .__logger .warning (lambda : f"Response for { message } is already done." )
621
597
622
598
except (SystemExit , KeyboardInterrupt ):
623
599
raise
624
600
except BaseException as e :
625
601
if not entry .future .done ():
626
- if entry .future .get_loop () == asyncio .get_running_loop ():
627
- entry .future .set_exception (e )
628
- else :
629
- if entry .future .get_loop ().is_running ():
630
- entry .future .get_loop ().call_soon_threadsafe (entry .future .set_exception , e )
602
+ entry .future .set_exception (e )
631
603
632
604
@__logger .call
633
605
async def handle_error (self , message : JsonRPCError ) -> None :
@@ -646,50 +618,17 @@ async def handle_error(self, message: JsonRPCError) -> None:
646
618
647
619
try :
648
620
if not entry .future .done ():
649
- res = None
650
- if message .result is not None :
651
- res = from_dict (message .result , entry .result_type )
652
- if entry .future .get_loop () == asyncio .get_running_loop ():
653
- entry .future .set_exception (
654
- JsonRPCErrorException (message .error .code , message .error .message , message .error .data )
655
- )
656
- else :
657
- if entry .future .get_loop ().is_running ():
658
-
659
- def set_result (f : asyncio .Future [Any ], r : Any , ev : threading .Event ) -> None :
660
- try :
661
- if not f .done () and f .get_loop ().is_running ():
662
- f .set_exception (
663
- JsonRPCErrorException (
664
- message .error .code , message .error .message , message .error .data
665
- )
666
- )
667
- finally :
668
- ev .set ()
669
-
670
- done = threading .Event ()
671
-
672
- entry .future .get_loop ().call_soon_threadsafe (set_result , entry .future , res , done )
673
-
674
- start = time .monotonic ()
675
- while not done .is_set ():
676
- if time .monotonic () - start > 120 :
677
- raise TimeoutError ("Can't set future result." )
678
-
679
- await asyncio .sleep (0 )
680
-
681
- else :
682
- self .__logger .warning (lambda : f"Response { entry !r} loop is not running." )
621
+ entry .future .set_exception (
622
+ JsonRPCErrorException (message .error .code , message .error .message , message .error .data )
623
+ )
624
+ else :
625
+ self .__logger .warning (lambda : f"Response for { message } is already done." )
683
626
684
627
except (SystemExit , KeyboardInterrupt ):
685
628
raise
686
629
except BaseException as e :
687
630
if not entry .future .done ():
688
- if entry .future .get_loop () == asyncio .get_running_loop ():
689
- entry .future .set_exception (e )
690
- else :
691
- if entry .future .get_loop ().is_running ():
692
- entry .future .get_loop ().call_soon_threadsafe (entry .future .set_exception , e )
631
+ entry .future .set_exception (e )
693
632
694
633
@staticmethod
695
634
def _convert_params (
0 commit comments