首页 > 解决方案 > 等待上一个函数在Python中完成

问题描述

python中是否有任何解决方案可以让函数在前一个函数完成后执行?

这是我现在正在使用的想法之一。但是当文件较大并且程序需要更多时间时,它并不能解决问题。

def copy_to_jumphost(self):
        try:
            if self.connect():
                stdin, stdout, stderr = self.client.exec_command('sshpass -p %s scp -r %s@%s:%s/' % (self.password,self.username,self.hostname,self.log_path) + self.lista[self.file_number].rstrip() + ' ' + '/home/%s/' % (self.username) + self.lista[self.file_number].rstrip())   
        except (AttributeError, TypeError) as e:
            print("Error occurred:", e)
try:
    if self.connect():
        if self.copy_to_jumphost():
            ftp_client = self.client.open_sftp()
            ftp_client.get(filepath, self.localpath)
            print("Success! \nFile coppied to %s" %(self.localpath))
        else: 
            time.sleep(5)
            ftp_client = self.client.open_sftp()
            ftp_client.get(filepath, self.localpath)
            print("Success but needed some time! \nFile coppied to %s" %(self.localpath))
except (AttributeError, TypeError) as e:
    print("Error occurred:", e)

对我来说完美的情况是如果在 else 语句中有一个等待完成copy_to_jumphost()功能的解决方案,因为time.sleep(5)如果我需要复制更大的文件将会失败。

标签: pythonfunctionwaitscp

解决方案


推荐阅读