大家能不能帮我分析一下,这段代码为什么会报错:
我写了一个从远程主要下载图片的类:
class DownloadTask(QThread):
all_done = pyqtSignal()
def __init__(self, file_list, local_dir):
super().__init__()
self.file_list = file_list
self.local_dir = local_dir
self.semaphore = QSemaphore(5) # 最大并发数
self.completed = 0
def run(self):
if not self.file_list:
self.all_done.emit()
return
threads = []
for i, remote_path in enumerate(self.file_list):
file_name=os.path.basename(remote_path)
file_local_path=os.path.join(self.local_dir,file_name)
thread = SingleDownload(remote_path, file_local_path, self.semaphore)
thread.finished.connect(self.on_file_done)
threads.append(thread)
thread.start()
for thread in threads:
thread.wait()
self.all_done.emit()
def on_file_done(self):
self.completed += 1
# print(f"完成: {self.completed}/{len(self.file_list)}")
class SingleDownload(QThread):
finished = pyqtSignal()
def __init__(self, remote_path, local_path, semaphore):
super().__init__()
self.remote_path = remote_path
self.local_path = local_path
self.semaphore = semaphore
def run(self):
self.semaphore.acquire()
try:
# SSH下载逻辑
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(C.SERVER_HOST, username=C.SERVER_USER, password=C.SERVER_PASSWORD)
sftp = ssh.open_sftp()
os.makedirs(os.path.dirname(self.local_path), exist_ok=True)
sftp.get(self.remote_path, self.local_path)
sftp.close()
ssh.close()
except Exception as err:
show_error_dialog(self,'远程访问失败!',f'{err}')
print(err)
finally:
self.semaphore.release()
self.finished.emit()
show_error_dialog是从其它模块导入的:
```python
def show_error_dialog(parent,title,message):
"""显示错误对话框"""
QMessageBox.critical(parent, title, message)
print_logs(resource_path(C.LOG_FILE_ADDRESS),message)
main文件中的调用语句为:
self.down = DownloadTask(download_images_address, root_dir)
现在下载出错时show_error_dialog(self,'远程访问失败!',f'{err}')会报错,我没明白是哪里有问题。