python - 等待上一个函数在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)
如果我需要复制更大的文件将会失败。
解决方案
推荐阅读
- python - Python - 当 .xlsx 文件发送到打印机时收到通知
- java - 如何随机生成这样的值 23-4564567
- php - 如何使用带有 blueimp 上传代码的 $_REQUEST 获取文件名
- ios - 如何在 iOS 上审核/记录文件访问事件
- javascript - 修改 CountUp jQuery 以从滚动点开始
- reactjs - React-如何根据父状态更新子道具
- laravel - Laravel 中的 attributesToArray() 和 toArray() 有什么区别?
- docker - 主机名不适用于 docker swarm 模式
- android - Android.Views.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class android.support.design.widget.NavigationView
- mysql - 如何在我的视图 mysql 中更改和添加“id”列