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 |
| 12 | import gen_print as gp |
| 13 | |
| 14 | |
| 15 | ############################################################################### |
| 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. |
| 24 | return_dict A dictionary consisting of pid/process output for the |
| 25 | keys/values. This function will append a new entry to |
| 26 | this dictionary. |
| 27 | """ |
| 28 | |
| 29 | pid = os.getpid() |
| 30 | output = BuiltIn().run_keyword(keyword_name) |
| 31 | |
| 32 | # Build PID:<output> dictionary. |
| 33 | return_dict[str(pid)] = str(output) |
| 34 | |
| 35 | ############################################################################### |
| 36 | |
| 37 | |
| 38 | ############################################################################### |
| 39 | def execute_process(num_process, keyword_name): |
| 40 | r""" |
| 41 | Execute a robot keyword via multiprocessing process. |
| 42 | |
| 43 | Description of argument(s): |
| 44 | num_process Number of times keyword to be executed. |
| 45 | keyword_name Keyword name to be executed. |
| 46 | """ |
| 47 | |
| 48 | manager = Manager() |
| 49 | return_dict = manager.dict() |
| 50 | process_list = [] |
| 51 | |
| 52 | # Append user-defined times process needed to execute. |
| 53 | for ix in range(int(num_process)): |
| 54 | task = Process(target=execute_keyword, args=(keyword_name, return_dict)) |
| 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 |
| 64 | |
| 65 | ############################################################################### |