Boto3 S3资源卡在“ Object.get”方法上

时间:2019-08-18 06:57:57

标签: python amazon-web-services amazon-s3 boto3

我尝试使用boto3库的“ Object.get()”方法从S3资源中获取一个pickle文件,该方法同时从多个进程中从中获取。这会导致我的程序卡在某个进程中(没有引发异常,该程序不会继续到下一行)。

我试图在S3连接中添加一个“ Config”变量。那没有帮助。

import pickle
import boto3
from botocore.client import Config

s3_item = _get_s3_name(descriptor_key)  # Returns a path string of the desiered file
config = Config(connect_timeout=5, retries={'max_attempts': 0})
s3 = boto3.resource('s3', config=config)
bucket_uri = os.environ.get(*ct.S3_MICRO_SERVICE_BUCKET_URI)  # Returns a string of the bucket URI
estimator_factory_logger.debug(f"Calling s3 with item {s3_item} from URI {bucket_uri}")
model_file_from_s3 = s3.Bucket(bucket_uri).Object(s3_item)
estimator_factory_logger.debug("Loading bytes...")
model_content = model_file_from_s3.get()['Body'].read()  # <- Program gets stuck here
estimator_factory_logger.debug("Loading from pickle...")
est = pickle.loads(model_content)

没有错误消息。似乎“获取”方法陷入了僵局。

非常感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

存储桶中的文件之一可能很大,程序读取时间很长吗?

在这种情况下,作为调试步骤,我将研究model_file_from_s3.get()['Body']对象botocore.response.StreamingBody对象,并在其上使用set_socket_timeout()尝试强制超时。

https://botocore.amazonaws.com/v1/documentation/api/latest/reference/response.html

答案 1 :(得分:0)

问题是我们在主进程中打开了多个线程之后创建了一个子进程。显然,这在Linux中是一个很大的禁忌。 我们通过使用“ spawn”而不是“ fork”来修复它