【发布时间】:2017-02-14 00:43:11
【问题描述】:
理想情况下,我想做的是在 python 中复制这个 bash 管道(我在这里使用cut 来表示数据的一些任意转换。我实际上想使用pandas 来执行此操作):
curl ftp://hgdownload.cse.ucsc.edu/goldenPath/hg38/database/refFlat.txt.gz | gunzip | cut -f 1,2,4
我可以用python写如下代码,达到同样的目的
# 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.retrbinary() 调用会阻塞,并等待整个下载。我想要的是有一个长二进制流,以 FTP 文件作为源,gunzip 作为过滤器,pd.read_csv() 作为接收器,所有这些都同时处理数据,就像在我的 bash 管道中一样。有没有办法阻止retrbinary() 阻止?
我意识到这可能是不可能的,因为 python 不能使用多个线程。这是真的?如果是这样,我可以使用multiprocessing 或async 或其他语言功能来实现此同步管道
编辑:将 storbinary 更改为 retrbinary,这是一个错字,问题仍然存在
【问题讨论】:
-
Python 完全可以使用多线程。您正在考虑 GIL,这是不同的。
标签: python ftp stream pipe ftplib