尝试使用python ftplib从NCBI ftp下载一系列存档,但ftplib在长时间文件传输结束时冻结

尝试使用python ftplib从NCBI ftp下载一系列存档,但ftplib在长时间文件传输结束时冻结,python,ftplib,ncbi,Python,Ftplib,Ncbi,我正在尝试下载nr blastdabase表单NCBIs ftp站点()。其中一个文件相当大(16GB),下载需要一些时间。在下载完这个文件后,程序就挂起了,它不会移动到下一个文件 我的程序中与下载文件相关的部分是: from pathlib import Path import ftplib from tqdm import tqdm def _file_write_progress(block, fh, pbar): """Write ftp file

我正在尝试下载nr blastdabase表单NCBIs ftp站点()。其中一个文件相当大(16GB),下载需要一些时间。在下载完这个文件后,程序就挂起了,它不会移动到下一个文件

我的程序中与下载文件相关的部分是:

from pathlib import Path
import ftplib
from tqdm import tqdm

def _file_write_progress(block, fh, pbar):
    """Write ftp file and updates progress bar.

    Args:
        block (binary): Block of data received from ftp.retrbinary
        fh (BufferedWriter): Open file to write to in wb mode
        pbar (ProgressBar): Progress bar to update with download progress
    """
    fh.write(block)
    pbar.update(len(block))


def _download_ftp_files(url, remote_path, files_list, db_dir):
    """Download ftp file and update progress bar.

    Args:
        url (str): Url of ftp server to connect to
        remote_path (str): Path to directory containing tartget files
        files_list (list(str)): List of files to download
        db_dir (Path): Path to local directory to download files to
    """
    ftp = ftplib.FTP(url, timeout=3600)
    ftp.login()
    ftp.cwd(remote_path)
    for fn in tqdm(files_list, desc="Downloading file #"):
        with (db_dir / fn).open('wb') as fh:
            pbar = tqdm(desc=fn, total=ftp.size(fn))
            ftp.retrbinary(
                'RETR ' + fn,
                lambda block: _file_write_progress(block, fh, pbar),
                1024*1024
            )
    ftp.close()
我认为这个问题与ftp连接超时有关,但我似乎无法修复它

我在和尝试过解决方案,但似乎无法使其发挥作用

根据这些答案修改了上述代码:

def _background(sock, fh, pbar):
    while True:
        block = sock.recv(1024*1024)
        if not block:
            break
        fh.write(block)
        pbar.update(len(block))


def _download_ftp_files(url, remote_path, files_list, db_dir):
    ftp = ftplib.FTP(url)
    ftp.login()
    ftp.cwd(remote_path)
    for fn in tqdm(files_list, desc="Downloading file #"):
        try:
            sock, size = ftp.ntransfercmd('RETR ' + fn)
            pbar = tqdm(desc=fn, total=size)
            with (db_dir / fn).open('wb') as fh:
                t = threading.Thread(target=_background(sock, fh, pbar))
                t.start()
                while t.is_alive():
                    t.join(60)
                    ftp.voidcmd('NOOP')
            sock.close()
        except ftplib.error_reply as e:
            print(e)
这将返回ftplib.error_reply 226由于某种原因而作为异常完成的传输。我试图处理它,但程序只是冻结


我可以提供更多的信息,如果需要,任何帮助是非常感谢

好吧,我换成了ftputil,它包装了ftplib,目前似乎效果更好

以下是修改后的代码:

def _download_ftp_files(url, remote_path, files_list, db_dir):
"""Download ftp file and update progress bar.

Args:
    url (str): URL of ftp server to connect to
    remote_path (str): Path to directory containing tartget files
    files_list (list(str)): List of files to download
    db_dir (Path): Path to local directory to download files to
"""
with ftputil.FTPHost(url, user='anonymous', passwd='@anonymous') as ftp_host:
    ftp_host.chdir(remote_path)
    for fn in tqdm(files_list, desc="Downloading file #"):
        pbar = tqdm(desc=fn, total=ftp_host.path.getsize(fn))
        ftp_host.download(
            fn, str(db_dir / fn),
            lambda block: pbar.update(len(block)))