1#!/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
15def 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.
23    return_dict     A dictionary consisting of pid/process status for the
24                    keys/values. This function will append a new entry to
25                    this dictionary.
26    """
27
28    pid = os.getpid()
29    status = BuiltIn().run_keyword_and_return_status(keyword_name)
30
31    # Build PID:<status> dictionary.
32    return_dict[str(pid)] = str(status)
33
34
35def 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)):
50        task = Process(target=execute_keyword,
51                       args=(keyword_name, return_dict))
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
61