blob: ea9bc67b11a02d68b05606817227d2a423705bea [file] [log] [blame]
George Keishingd942b512017-06-06 14:34:55 -05001#!/usr/bin/env python
2
3r"""
4This module contains keyword functions to support multiprocessing
5execution of keywords where generic robot keywords don't support.
6
7"""
8
9from robot.libraries.BuiltIn import BuiltIn
10from multiprocessing import Process, Manager
11import os
12import gen_print as gp
13
14
George Keishingd942b512017-06-06 14:34:55 -050015def 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 Keishing84d089f2017-07-17 09:33:01 -050023 return_dict A dictionary consisting of pid/process status for the
George Keishingd942b512017-06-06 14:34:55 -050024 keys/values. This function will append a new entry to
25 this dictionary.
26 """
27
28 pid = os.getpid()
George Keishing84d089f2017-07-17 09:33:01 -050029 status = BuiltIn().run_keyword_and_return_status(keyword_name)
George Keishingd942b512017-06-06 14:34:55 -050030
George Keishing84d089f2017-07-17 09:33:01 -050031 # Build PID:<status> dictionary.
32 return_dict[str(pid)] = str(status)
George Keishingd942b512017-06-06 14:34:55 -050033
George Keishingd942b512017-06-06 14:34:55 -050034
George Keishingd942b512017-06-06 14:34:55 -050035def 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 Mills096cd562018-03-26 10:19:12 -050050 task = Process(target=execute_keyword,
51 args=(keyword_name, return_dict))
George Keishingd942b512017-06-06 14:34:55 -050052 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