66
77class UtilityKeywords (LibraryComponent ):
88
9+ @keyword
10+ def run_async_keywords_and_return_first_completed (self , * keywords ):
11+ """Executes all the given keywords in a asynchronous and wait until first keyword is completed
12+
13+ ``Return`` Array of result for each keywords based on index
14+
15+ Example
16+ | `Run Async Keywords And Return First Completed` | Wait for response url | ${HOME_PAGE_URL}/login.html | AND |
17+ | ... | Wait for response url | ${HOME_PAGE_URL}/home.html | |
18+ """
19+ self .ctx .load_async_keywords ()
20+ run_keyword = _RunKeyword ()
21+ return self .loop .run_until_complete ( self ._run_async_keywords_first_completed (run_keyword ._split_run_keywords (list (keywords ))) )
22+
23+ async def _wrapped_async_keyword_return_index (self , index , future ):
24+ await future
25+ return index
26+
27+ async def _run_async_keywords_first_completed (self , iterable ):
28+ org_statements = []
29+ index = 0
30+ for kw , args in iterable :
31+ kw_name = kw .lower ().replace (' ' , '_' ) + '_async'
32+ org_statements .append (self ._wrapped_async_keyword_return_index (index , self .ctx .keywords [kw_name ](* args )))
33+ index += 1
34+ statements = org_statements
35+ error_stack_trace = ''
36+ while True :
37+ done , pending = await asyncio .wait (statements , return_when = asyncio .FIRST_COMPLETED )
38+ statements = pending
39+ for future in done :
40+ try :
41+ # Raise an exception if coroutine failed
42+ result_index = future .result ()
43+ # Force cancel all pending
44+ for p in pending :
45+ p .cancel ()
46+ return result_index
47+ except Exception as e :
48+ error_stack_trace += str (e )+ '\n '
49+ continue
50+ if len (pending ) == 0 :
51+ raise Exception ("All async keywords failed \r \n " + error_stack_trace )
52+
953 @keyword
1054 def run_async_keywords (self , * keywords ):
11- # Ensure that script load async keywords before run async keywords function
1255 """Executes all the given keywords in a asynchronous and wait until all keyword is completed
1356
14- ``Return`` Array of return for reach keywords based on index
57+ ``Return`` Array of result for each keywords based on index
1558
1659 Example:
17-
1860 | Open browser | ${HOME_PAGE_URL} | options=${options} | |
1961 | `Run Async Keywords` | Click Element | id:login_button | AND |
2062 | ... | Wait for response url | ${HOME_PAGE_URL}/home.html | |
@@ -23,7 +65,7 @@ def run_async_keywords(self, *keywords):
2365 self .ctx .load_async_keywords ()
2466 run_keyword = _RunKeyword ()
2567 return self .loop .run_until_complete ( self ._run_async_keywords (run_keyword ._split_run_keywords (list (keywords ))) )
26-
68+
2769 async def _run_async_keywords (self , iterable ):
2870 statements = []
2971 for kw , args in iterable :
0 commit comments