使用Python的ftplib进行管道连接而不会阻塞

时间:2017-02-14 00:43:11

标签: python ftp stream pipe ftplib

理想情况下,我想要做的是在python中复制这个bash管道(我在这里使用cut代表一些任意的数据转换。我实际上想要使用{{1这样做):

pandas

我可以在python中编写以下代码,实现相同的目标

curl ftp://hgdownload.cse.ucsc.edu/goldenPath/hg38/database/refFlat.txt.gz | gunzip | cut -f 1,2,4

然而,# Download the zip file into memory file = io.BytesIO() ftp = ftplib.FTP('hgdownload.cse.ucsc.edu') ftp.retrbinary(f'RETR goldenPath/{args.reference}/database/refFlat.txt.gz', file.write) # Unzip the gzip file table = gzip.GzipFile(fileobj=file) # Read into pandas df = pd.read_csv(table) 调用阻塞,并等待整个下载。我想要的是有一个长二进制流,FTP文件作为源,一个ftp.retrbinary()作为过滤器,gunzip作为接收器,所有同时处理数据,如我的bash管道。有没有办法阻止pd.read_csv()阻止?

我意识到这可能是不可能的,因为python不能使用多个线程。这是真的?如果是,我可以使用retrbinary()multiprocessing或其他语言功能来实现此同步管道

编辑:将async更改为storbinary,这是一个错字,问题仍然存在

1 个答案:

答案 0 :(得分:0)

您应该可以直接将文件下载到GZipFile

gzipfile = gzip.GzipFile()
ftp.storbinary(f'RETR goldenPath/{args.reference}/database/refFlat.txt.gz', gzipfile.write)
相关问题