(params, requests)
| 942 | |
| 943 | |
| 944 | def execute_batch(params, requests): |
| 945 | # type: (KeeperParams, List[dict]) -> List[dict] |
| 946 | responses = [] |
| 947 | if not requests: |
| 948 | return responses |
| 949 | |
| 950 | throttle_delay = 10 |
| 951 | chunk_size = 999 |
| 952 | queue = requests.copy() |
| 953 | delay_next_batch = False |
| 954 | |
| 955 | while len(queue) > 0: |
| 956 | chunk = queue[:chunk_size] |
| 957 | queue = queue[chunk_size:] |
| 958 | |
| 959 | rq = { |
| 960 | 'command': 'execute', |
| 961 | 'requests': chunk |
| 962 | } |
| 963 | try: |
| 964 | if delay_next_batch: |
| 965 | time.sleep(throttle_delay) |
| 966 | rs = communicate(params, rq) |
| 967 | if 'results' in rs: |
| 968 | results = rs['results'] # type: list |
| 969 | if len(results) > 0: |
| 970 | error_rs = results[-1] |
| 971 | throttled = error_rs.get('result') != 'success' and error_rs.get('result_code') == 'throttled' |
| 972 | if throttled: |
| 973 | delay_next_batch = True |
| 974 | results.pop() |
| 975 | responses.extend(results) |
| 976 | |
| 977 | if len(results) < len(chunk): |
| 978 | queue = chunk[len(results):] + queue |
| 979 | except Exception as e: |
| 980 | logging.error(e) |
| 981 | |
| 982 | return responses |
| 983 | |
| 984 | |
| 985 | def update_record(params, record, **kwargs): |
no test coverage detected