FTP下载冻结了整个应用程序 [英] FTP downloading freezes the whole application

查看:74
本文介绍了FTP下载冻结了整个应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从FTP服务器下载大约100 MB的文件.这是一个测试.bin文件,因为我正在测试该应用程序,并且我猜想将来要下载的文件的权重会更大.当我要下载文件时,整个应用程序将冻结,然后在几秒钟后下载文件.该文件已完成,可以成功下载,没有任何错误或类似错误.唯一的问题是冻结.

I'm trying to download a file from FTP server which is about 100 MB. It's a test .bin file because I'm testing the app and I guess the files I will want to download in the future will weight even more. When I want to download the file the whole application just freezes and then after a few seconds it downloads the file. The file is complete and it downloads it successfully without any errors or something like that. The only problem is the freeze.

我的代码:

ftp = FTP('exampledomain.com')
ftp.login(user='user', passwd='password')

ftp.cwd('/main_directory/')

filename = '100MB.bin'

with open(filename, 'wb') as localfile:
    ftp.retrbinary('RETR ' + filename, localfile.write, 1024)

ftp.quit()
localfile.close()

我尝试在代码的某些行之间(例如,在登录和下载文件之间)使用sleep(),但是它没有帮助,而且看来sleep()在使用FTP时根本无法正常工作.

I tried using sleep() between some lines of the code e.g between the logging in and downloading the file but it didn't help and it also seems that sleep() does not work AT ALL when working with FTP.

推荐答案

在另一个线程上下载文件:

Download the file on another thread:

import threading
import time
from ftplib import FTP

def download():
    ftp = FTP('exampledomain.com')
    ftp.login(user='user', passwd='password')

    ftp.cwd('/main_directory/')

    filename = '100MB.bin'

    with open(filename, 'wb') as localfile:
        ftp.retrbinary('RETR ' + filename, localfile.write)

    ftp.quit()

print("Starting download...")

thread = threading.Thread(target=download)
thread.start()

print("Download started")

while thread.isAlive():
    print("Still downloading...")
    time.sleep(1)

print("Done")


基于:
如何使用Python 2.7通过具有多线程(异步下载)的HTTP通过HTTP下载文件


Based on:
How to download a file over HTTP with multi-thread (asynchronous download) using Python 2.7

您有关修改PyQt代码的后续问题:
带有文本标签的FTP下载,其中显示了下载的当前状态

Your followup question about modifying the code for PyQt:
FTP download with text label showing the current status of the download

这篇关于FTP下载冻结了整个应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆