| 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 |  | 
| George Keishing | d942b51 | 2017-06-06 14:34:55 -0500 | [diff] [blame] | 15 | def execute_keyword(keyword_name, return_dict): | 
|  | 16 | r""" | 
|  | 17 | Execute a robot keyword. | 
|  | 18 | In addition to running the caller's keyword, this function will: | 
|  | 19 | - Add an entry to the return_dict | 
|  | 20 |  | 
|  | 21 | Description of argument(s): | 
|  | 22 | keyword_name    Keyword name to be executed. | 
| George Keishing | 84d089f | 2017-07-17 09:33:01 -0500 | [diff] [blame] | 23 | return_dict     A dictionary consisting of pid/process status for the | 
| George Keishing | d942b51 | 2017-06-06 14:34:55 -0500 | [diff] [blame] | 24 | keys/values. This function will append a new entry to | 
|  | 25 | this dictionary. | 
|  | 26 | """ | 
|  | 27 |  | 
|  | 28 | pid = os.getpid() | 
| George Keishing | 84d089f | 2017-07-17 09:33:01 -0500 | [diff] [blame] | 29 | status = BuiltIn().run_keyword_and_return_status(keyword_name) | 
| George Keishing | d942b51 | 2017-06-06 14:34:55 -0500 | [diff] [blame] | 30 |  | 
| George Keishing | 84d089f | 2017-07-17 09:33:01 -0500 | [diff] [blame] | 31 | # Build PID:<status> dictionary. | 
|  | 32 | return_dict[str(pid)] = str(status) | 
| George Keishing | d942b51 | 2017-06-06 14:34:55 -0500 | [diff] [blame] | 33 |  | 
| 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 | def execute_process(num_process, keyword_name): | 
|  | 36 | r""" | 
|  | 37 | Execute a robot keyword via multiprocessing process. | 
|  | 38 |  | 
|  | 39 | Description of argument(s): | 
|  | 40 | num_process         Number of times keyword to be executed. | 
|  | 41 | keyword_name     Keyword name to be executed. | 
|  | 42 | """ | 
|  | 43 |  | 
|  | 44 | manager = Manager() | 
|  | 45 | return_dict = manager.dict() | 
|  | 46 | process_list = [] | 
|  | 47 |  | 
|  | 48 | # Append user-defined times process needed to execute. | 
|  | 49 | for ix in range(int(num_process)): | 
| Gunnar Mills | 096cd56 | 2018-03-26 10:19:12 -0500 | [diff] [blame] | 50 | task = Process(target=execute_keyword, | 
|  | 51 | args=(keyword_name, return_dict)) | 
| George Keishing | d942b51 | 2017-06-06 14:34:55 -0500 | [diff] [blame] | 52 | process_list.append(task) | 
|  | 53 | task.start() | 
|  | 54 |  | 
|  | 55 | # Wait for process to complete. | 
|  | 56 | for task in process_list: | 
|  | 57 | task.join() | 
|  | 58 |  | 
|  | 59 | # Return function return codes. | 
|  | 60 | return return_dict |