1e7e9171eSGeorge Keishing#!/usr/bin/env python3
2d942b518SGeorge Keishing
3d942b518SGeorge Keishingr"""
4d942b518SGeorge KeishingThis module contains keyword functions to support multiprocessing
5d942b518SGeorge Keishingexecution of keywords where generic robot keywords don't support.
6d942b518SGeorge Keishing
7d942b518SGeorge Keishing"""
8d942b518SGeorge Keishing
9e635ddc0SGeorge Keishingimport datetime
1020f38712SPatrick Williamsimport os
1120f38712SPatrick Williamsfrom multiprocessing import Manager, Process
1220f38712SPatrick Williams
1320f38712SPatrick Williamsfrom robot.libraries.BuiltIn import BuiltIn
14d942b518SGeorge Keishing
15d942b518SGeorge Keishing
16d942b518SGeorge Keishingdef execute_keyword(keyword_name, return_dict):
17d942b518SGeorge Keishing    r"""
18d942b518SGeorge Keishing    Execute a robot keyword.
19d942b518SGeorge Keishing    In addition to running the caller's keyword, this function will:
20d942b518SGeorge Keishing    - Add an entry to the return_dict
21d942b518SGeorge Keishing
22d942b518SGeorge Keishing    Description of argument(s):
23d942b518SGeorge Keishing    keyword_name    Keyword name to be executed.
2484d089f2SGeorge Keishing    return_dict     A dictionary consisting of pid/process status for the
25d942b518SGeorge Keishing                    keys/values. This function will append a new entry to
26d942b518SGeorge Keishing                    this dictionary.
27d942b518SGeorge Keishing    """
28d942b518SGeorge Keishing
29d942b518SGeorge Keishing    pid = os.getpid()
3084d089f2SGeorge Keishing    status = BuiltIn().run_keyword_and_return_status(keyword_name)
31d942b518SGeorge Keishing
3284d089f2SGeorge Keishing    # Build PID:<status> dictionary.
3384d089f2SGeorge Keishing    return_dict[str(pid)] = str(status)
34d942b518SGeorge Keishing
35d942b518SGeorge Keishing
36d942b518SGeorge Keishingdef execute_process(num_process, keyword_name):
37d942b518SGeorge Keishing    r"""
38d942b518SGeorge Keishing    Execute a robot keyword via multiprocessing process.
39d942b518SGeorge Keishing
40d942b518SGeorge Keishing    Description of argument(s):
41d942b518SGeorge Keishing    num_process         Number of times keyword to be executed.
42d942b518SGeorge Keishing    keyword_name     Keyword name to be executed.
43d942b518SGeorge Keishing    """
44d942b518SGeorge Keishing
45d942b518SGeorge Keishing    manager = Manager()
46d942b518SGeorge Keishing    return_dict = manager.dict()
47d942b518SGeorge Keishing    process_list = []
48d942b518SGeorge Keishing
49d942b518SGeorge Keishing    # Append user-defined times process needed to execute.
503f7ef6deSSandhya Somashekar    for _ix in range(int(num_process)):
5120f38712SPatrick Williams        task = Process(
5220f38712SPatrick Williams            target=execute_keyword, args=(keyword_name, return_dict)
5320f38712SPatrick Williams        )
54d942b518SGeorge Keishing        process_list.append(task)
55d942b518SGeorge Keishing        task.start()
56d942b518SGeorge Keishing
57d942b518SGeorge Keishing    # Wait for process to complete.
58d942b518SGeorge Keishing    for task in process_list:
59d942b518SGeorge Keishing        task.join()
60d942b518SGeorge Keishing
61d942b518SGeorge Keishing    # Return function return codes.
62d942b518SGeorge Keishing    return return_dict
635fc20cb9Sshrsuman123
645fc20cb9Sshrsuman123
655fc20cb9Sshrsuman123def execute_keyword_args(keyword_name, args, return_dict):
665fc20cb9Sshrsuman123    r"""
675fc20cb9Sshrsuman123    Execute a robot keyword with arguments.
685fc20cb9Sshrsuman123    In addition to running the caller's keyword, this function will:
695fc20cb9Sshrsuman123    - Add an entry to the return_dict
705fc20cb9Sshrsuman123    Description of argument(s):
715fc20cb9Sshrsuman123    keyword_name    Keyword name to be executed.
725fc20cb9Sshrsuman123    args            Arguments to keyword.
735fc20cb9Sshrsuman123    return_dict     A dictionary consisting of pid/process status for the
745fc20cb9Sshrsuman123                    keys/values. This function will append a new entry to
755fc20cb9Sshrsuman123                    this dictionary.
765fc20cb9Sshrsuman123    """
775fc20cb9Sshrsuman123
785fc20cb9Sshrsuman123    execution_time = datetime.datetime.now()
795fc20cb9Sshrsuman123
805fc20cb9Sshrsuman123    status = BuiltIn().run_keyword_and_return_status(keyword_name, *args)
815fc20cb9Sshrsuman123
825fc20cb9Sshrsuman123    # Build execution time:<status> dictionary.
835fc20cb9Sshrsuman123    return_dict[str(execution_time)] = str(status)
845fc20cb9Sshrsuman123
855fc20cb9Sshrsuman123
865fc20cb9Sshrsuman123def execute_process_multi_keyword(number_args, *keyword_names):
875fc20cb9Sshrsuman123    r"""
885fc20cb9Sshrsuman123    Execute multiple robot keywords with arguments via multiprocessing process.
895fc20cb9Sshrsuman123
905fc20cb9Sshrsuman123    Description of argument(s):
915fc20cb9Sshrsuman123    number_args       Number of argument in keywords.
925fc20cb9Sshrsuman123    keyword_names     Keyword name to be executed.
935fc20cb9Sshrsuman123    """
945fc20cb9Sshrsuman123
955fc20cb9Sshrsuman123    manager = Manager()
965fc20cb9Sshrsuman123    return_dict = manager.dict()
975fc20cb9Sshrsuman123    process_list = []
985fc20cb9Sshrsuman123    # Append each keyword with its arguments in a process to execute.
995fc20cb9Sshrsuman123    for keywords_data in keyword_names:
1005fc20cb9Sshrsuman123        keyword_args = tuple(keywords_data.split(" ")[-number_args:])
1015fc20cb9Sshrsuman123        keyword_name = " ".join(keywords_data.split(" ")[:-number_args])
10220f38712SPatrick Williams        task = Process(
10320f38712SPatrick Williams            target=execute_keyword_args,
10420f38712SPatrick Williams            args=(keyword_name, keyword_args, return_dict),
10520f38712SPatrick Williams        )
1065fc20cb9Sshrsuman123        process_list.append(task)
1075fc20cb9Sshrsuman123        task.start()
1085fc20cb9Sshrsuman123
1095fc20cb9Sshrsuman123    # Wait for process to complete.
1105fc20cb9Sshrsuman123    for task in process_list:
1115fc20cb9Sshrsuman123        task.join()
112*78e92170Skothais    return return_dict.copy()
1135fc20cb9Sshrsuman123
1145fc20cb9Sshrsuman123
1155fc20cb9Sshrsuman123def get_current_date_time():
1165fc20cb9Sshrsuman123    r"""
1175fc20cb9Sshrsuman123    Gets current time.
1185fc20cb9Sshrsuman123    """
1195fc20cb9Sshrsuman123
1205fc20cb9Sshrsuman123    current_time = datetime.datetime.now().strftime("%H:%M:%S.%f")
1215fc20cb9Sshrsuman123    return current_time
122