Python - connecting SSH wrappers to remote devices with threading

The problem I would like to solve is get the following:

  • instantiate 5-10 host ssh sessions simultaneously (threading??)
  • leave the connections established and awaiting for external calls
  • execute predifined methods under the Host class (such as getUberImportantDetails() )
  • expose information and selected methods for ssh wrappers with flask rest API.

    class Host():
        def __init__(self, host, user, psw):
            super().__init__(host, user, psw)
            self.bypass = ''
            self.state = "IDLE"
    
        def connect(self):
           self.ssh = paramiko.SSHClient()
           <a lot of regexp based SSH processing>
    
       def getUberImportantDetails(self):
            output = self.execute("show very important data")
        return SRE4BGPNeighborParser(output)
    

Host's init() method has to get host ip address, username and password. Before getting any information out of the object i need to run the connect method to establish the SSH connection over a range of jumphosts. When Host class connect routing is finished, I am ready to interact with the remote device.

I was able to initialize the class object with a thread wrapper, but I also want to leave the threads running after connect() function done all the work and the handler is ready for interaction with the remote device. How do i do it nicely? Is there a better way to address my problem?

def threaded(fn):
    def wrapper(*args, **kwargs):
        thread = threading.Thread(target=fn, args=args, kwargs=kwargs)
        thread.start()
        return thread
    return wrapper


class ThreadHost(threading.Thread, Host):
    def __init__(self, host, user, psw):
        super(Host, self).__init__(host, user, psw)
        self.bypass = ''
        self.state = "IDLE"

    @threaded
    def run(self):
        print("Starting connection to {}".format(self.host))
        self.connect()