Diwakar SHARMA
Diwakar SHARMA

Reputation: 581

Multithreading code is getting executed twice through Robot file

I am running a python multithreaded code which runs only once if i run it through python file but runs twice if i run it through Robot file :

python file code :

def connect():
        print("Step 12: Reload devices")
        config_threads_list = []
        ipAddress = '172.22.12.14'
        username = 'abcd'
        password = 'abcd'
        devices = ['5023','5024','5025','5026']
        for ports in devices:
            consoleServer, username, password, port = ipAddress, username, password, ports
            print ('Creating thread for: ', ports)
            config_threads_list.append(threading.Thread(target=obj.router_reload, args=(consoleServer, username, password, port)))
    
        print ('\n---- Begin get config threading ----\n')
        for config_thread in config_threads_list:
            config_thread.start()
    
        for config_thread in config_threads_list:
            config_thread.join()

connect()

this code works fine when i run it through python only . However when i run it through robot framework its running twice

robot file :

Documentation        Test case
Library             <path to above py file >

*** Test Cases ***
TEST CASE LEL-TC-1
    connect

Upvotes: 2

Views: 424

Answers (2)

Bence Kaulics
Bence Kaulics

Reputation: 7281

Just to share why it is executed twice when you run the robot file.

You call connect() at the end of the Python file. This is the single invocation when the Python script is executed.

Now when you import the Python file as a library its actually gets executed. So the connect() will be called at the end. That is one.

Then you call it explicitly as a keyword in the test case. That is two.

To avoid this simply remove the connect() call from the end of the Python file.

Upvotes: 3

Diwakar SHARMA
Diwakar SHARMA

Reputation: 581

Thanks to https://stackoverflow.com/users/4180176/joshua-nixon

after using the below mentioned simple/basic yet very effective code my issue has been resolved from robot file as well and code is only getting executed once :

if __name__ == "__main__":
   connect()

Upvotes: 2

Related Questions