python中从文本文件读入的每个元素的新线程

2024-04-20 12:15:47 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧,我有一个程序,是完全功能,但我想多线程它,使它更快,它所做的是更新一个框。现在它一次只做一个盒子,我想同时做所有的盒子。我对它做了一些研究,但没有什么能真正帮助我。下面是我的代码

 def createSSHClient(self,host,port,user,password):
       try:
         client = paramiko.SSHClient()
         client.load_system_host_keys()
         client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
         client.connect(host, port, user, password)
         return client
       except:
          print "could not connect"
          return "nothing"

 def Put(self,location, onloc, host, password, user, port):
      try:                                   
         ssh = self.createSSHClient(host, port, user, password)
         scp = SCPClient(ssh.get_transport())
         scp.put(location,onloc)
         print("Finshed Transfer")
         stdin , stdout, stderr = ssh.exec_command("path")
         print "going to start reboot"
         stdin,stdout,stderr = ssh.exec_command("path")
     except:    
         print "Lost connection"

 def push(self, Filename):
     location = Filename
     onloc = "/remote location"
     port = 22
     ct = 1
     with open("People.txt" , 'r') as inFile:    """Here is where will read in how many threads"""
           for line in inFile:
               ip,user,password = line.strip().split(',')
               self.Put(location,onloc,ip,password,user,port) """Here is where each thread should call"""
               print ("i finshed %d", ct)
               ct=+1

所以在我的评论中,我展示了我从哪里得到线程,以及我希望线程调用去哪里,有人能帮我吗。你知道吗


Tags: selfclienthostportdeflocationpasswordssh