George Keishing | e7e9171 | 2021-09-03 11:28:44 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
George Keishing | d942b51 | 2017-06-06 14:34:55 -0500 | [diff] [blame] | 2 | |
| 3 | r""" |
| 4 | This module contains keyword functions to support multiprocessing |
| 5 | execution of keywords where generic robot keywords don't support. |
| 6 | |
| 7 | """ |
| 8 | |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 9 | import datetime |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 10 | import os |
| 11 | from multiprocessing import Manager, Process |
| 12 | |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 13 | import gen_print as gp |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 14 | from robot.libraries.BuiltIn import BuiltIn |
George Keishing | d942b51 | 2017-06-06 14:34:55 -0500 | [diff] [blame] | 15 | |
| 16 | |
George Keishing | d942b51 | 2017-06-06 14:34:55 -0500 | [diff] [blame] | 17 | def execute_keyword(keyword_name, return_dict): |
| 18 | r""" |
| 19 | Execute a robot keyword. |
| 20 | In addition to running the caller's keyword, this function will: |
| 21 | - Add an entry to the return_dict |
| 22 | |
| 23 | Description of argument(s): |
| 24 | keyword_name Keyword name to be executed. |
George Keishing | 84d089f | 2017-07-17 09:33:01 -0500 | [diff] [blame] | 25 | return_dict A dictionary consisting of pid/process status for the |
George Keishing | d942b51 | 2017-06-06 14:34:55 -0500 | [diff] [blame] | 26 | keys/values. This function will append a new entry to |
| 27 | this dictionary. |
| 28 | """ |
| 29 | |
| 30 | pid = os.getpid() |
George Keishing | 84d089f | 2017-07-17 09:33:01 -0500 | [diff] [blame] | 31 | status = BuiltIn().run_keyword_and_return_status(keyword_name) |
George Keishing | d942b51 | 2017-06-06 14:34:55 -0500 | [diff] [blame] | 32 | |
George Keishing | 84d089f | 2017-07-17 09:33:01 -0500 | [diff] [blame] | 33 | # Build PID:<status> dictionary. |
| 34 | return_dict[str(pid)] = str(status) |
George Keishing | d942b51 | 2017-06-06 14:34:55 -0500 | [diff] [blame] | 35 | |
George Keishing | d942b51 | 2017-06-06 14:34:55 -0500 | [diff] [blame] | 36 | |
George Keishing | d942b51 | 2017-06-06 14:34:55 -0500 | [diff] [blame] | 37 | def execute_process(num_process, keyword_name): |
| 38 | r""" |
| 39 | Execute a robot keyword via multiprocessing process. |
| 40 | |
| 41 | Description of argument(s): |
| 42 | num_process Number of times keyword to be executed. |
| 43 | keyword_name Keyword name to be executed. |
| 44 | """ |
| 45 | |
| 46 | manager = Manager() |
| 47 | return_dict = manager.dict() |
| 48 | process_list = [] |
| 49 | |
| 50 | # Append user-defined times process needed to execute. |
| 51 | for ix in range(int(num_process)): |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 52 | task = Process( |
| 53 | target=execute_keyword, args=(keyword_name, return_dict) |
| 54 | ) |
George Keishing | d942b51 | 2017-06-06 14:34:55 -0500 | [diff] [blame] | 55 | process_list.append(task) |
| 56 | task.start() |
| 57 | |
| 58 | # Wait for process to complete. |
| 59 | for task in process_list: |
| 60 | task.join() |
| 61 | |
| 62 | # Return function return codes. |
| 63 | return return_dict |
shrsuman123 | 5fc20cb | 2021-02-02 04:55:47 -0600 | [diff] [blame] | 64 | |
| 65 | |
| 66 | def execute_keyword_args(keyword_name, args, return_dict): |
| 67 | r""" |
| 68 | Execute a robot keyword with arguments. |
| 69 | In addition to running the caller's keyword, this function will: |
| 70 | - Add an entry to the return_dict |
| 71 | Description of argument(s): |
| 72 | keyword_name Keyword name to be executed. |
| 73 | args Arguments to keyword. |
| 74 | return_dict A dictionary consisting of pid/process status for the |
| 75 | keys/values. This function will append a new entry to |
| 76 | this dictionary. |
| 77 | """ |
| 78 | |
| 79 | execution_time = datetime.datetime.now() |
| 80 | |
| 81 | status = BuiltIn().run_keyword_and_return_status(keyword_name, *args) |
| 82 | |
| 83 | # Build execution time:<status> dictionary. |
| 84 | return_dict[str(execution_time)] = str(status) |
| 85 | |
| 86 | |
| 87 | def execute_process_multi_keyword(number_args, *keyword_names): |
| 88 | r""" |
| 89 | Execute multiple robot keywords with arguments via multiprocessing process. |
| 90 | |
| 91 | Description of argument(s): |
| 92 | number_args Number of argument in keywords. |
| 93 | keyword_names Keyword name to be executed. |
| 94 | """ |
| 95 | |
| 96 | manager = Manager() |
| 97 | return_dict = manager.dict() |
| 98 | process_list = [] |
| 99 | # Append each keyword with its arguments in a process to execute. |
| 100 | for keywords_data in keyword_names: |
| 101 | keyword_args = tuple(keywords_data.split(" ")[-number_args:]) |
| 102 | keyword_name = " ".join(keywords_data.split(" ")[:-number_args]) |
Patrick Williams | 20f3871 | 2022-12-08 06:18:26 -0600 | [diff] [blame] | 103 | task = Process( |
| 104 | target=execute_keyword_args, |
| 105 | args=(keyword_name, keyword_args, return_dict), |
| 106 | ) |
shrsuman123 | 5fc20cb | 2021-02-02 04:55:47 -0600 | [diff] [blame] | 107 | process_list.append(task) |
| 108 | task.start() |
| 109 | |
| 110 | # Wait for process to complete. |
| 111 | for task in process_list: |
| 112 | task.join() |
| 113 | return return_dict |
| 114 | |
| 115 | |
| 116 | def get_current_date_time(): |
| 117 | r""" |
| 118 | Gets current time. |
| 119 | """ |
| 120 | |
| 121 | current_time = datetime.datetime.now().strftime("%H:%M:%S.%f") |
| 122 | return current_time |