简体   繁体   中英

Connect to multiple machines with threading in Python

I need to connect to multiple machines and execute set of commands on them. I want to implement this with threading but not sure how I can achieve this. Following is the code:

import threading

def conn_to_board(board_ip):
    # ssh connection to machine
    # set of commands

board_ip = ["1.1.1.1", "2.2.2.2", "3.3.3.3", "4.4.4.4"]

'''
for i in board_ip:
    t1 = threading.Thread(target=conn_to_board, args=(i,))
    t1.start()
    t1.join()
'''

Can someone help me in achieving this with threading?

It sounds like you're trying to re-invent Ansible or Salt . You might wish to investigate using one of these tools to accomplish your goal of running a set of shell commands on many machines. Both happen to be written in, and are extensible with, Python.

Assuming the function conn_to_board(board_ip) already does what you want and does not bind the same local port or exclusivelu uses a resource, multithreading is simple and your code is almost correct. The only problem I can see in your commented code is that you join each thread inside the loop actually serializing them one after the other - said differently the multi-threading is completely useless here.

You should first create and start all your threads (if the number is low enough) and then join them in a new loop:

...
thrs = [] # to store the threads
for i in board_ip:  # create and start the threads
    t1 = threading.Thread(target=conn_to_board, args=(i,))
    t1.start()
    thrs.append(t1)
for t1 in thrs:     # join all the threads
    t1.join()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM