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