【发布时间】:2020-12-04 02:57:30
【问题描述】:
在here 的讨论之后,以下代码可以即时读取和写入 s3:
from smart_open import open
import os
bucket_dir = "s3://my-bucket/annotations/"
with open(os.path.join(bucket_dir, "in.tsv.gz"), "rb") as fin:
with open(
os.path.join(bucket_dir, "out.tsv.gz"), "wb"
) as fout:
for line in fin:
l = [i.strip() for i in line.decode().split("\t")]
string = "\t".join(l) + "\n"
fout.write(string.encode())
问题是,在处理了几千行后(几分钟),我收到“对等连接重置”错误:
raise ProtocolError("Connection broken: %r" % e, e)
urllib3.exceptions.ProtocolError: ("Connection broken: ConnectionResetError(104, 'Connection reset by peer')", ConnectionResetError(104, 'Connection reset by peer'))
我能做什么?我在每个fout.write(string.encode()) 之后尝试fout.flush(),但效果不佳。是否有更好的解决方案来处理大约 2 亿行的 .tsv 文件?
【问题讨论】:
-
我正在使用 boto3,我目前遇到了同样的错误。我正在从 S3 存储桶中读取大量数据,并对其进行处理(大约需要几分钟)。处理后,当我返回读取下一个块时,出现上述错误。这是因为 S3 在其结束时终止会话。我们如何告诉 S3 让会话保持更长时间?
标签: python amazon-s3 python-s3fs