Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.5k views
in Technique[技术] by (71.8m points)

python - 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 ('
---- Begin get config threading ----
')
        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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...