简体   繁体   English

在boto3中诊断内存泄漏

[英]Diagnosing Memory leak in boto3

I have a celery worker running on Elastic Beanstalk that polls a SQS queue, gets messages (containing S3 file names), downloads those files from S3 and processes them. 我有一个在Elastic Beanstalk上运行的芹菜工作者,它轮询SQS队列,获取消息(包含S3文件名),从S3下载这些文件并处理它们。 My worker is scheduled to run at every 15 seconds but due to some reason the memory usage keeps on increasing with time. 我的工作人员计划每15秒运行一次,但由于某种原因,内存使用量会随着时间的推移而不断增加。

This is the code I'm using to access SQS 这是我用来访问SQS的代码

def get_messages_from_sqs(queue_url, queue_region="us-west-2", number_of_messages=1):
    client = boto3.client('sqs', region_name=queue_region)
    sqs_response = client.receive_message(QueueUrl=queue_url, MaxNumberOfMessages=number_of_messages)
    messages = sqs_response.get("Messages", [])
    cleaned_messages = []
    for message in messages:
        body = json.loads(message["Body"])
        data = body["Records"][0]
        data["receipt_handle"] = message["ReceiptHandle"]
        cleaned_messages.append(data)
    return cleaned_messages

def download_file_from_s3(bucket_name, filename):
    s3_client = boto3.client('s3')
    s3_client.download_file(bucket_name, filename, '/tmp/{}'.format(filename))

Do we need to close client connection in boto3 after we're done with the process ? 完成这个过程后,我们是否需要在boto3中关闭客户端连接? If so, how can we do it ? 如果是这样,我们怎么做呢? 内存监控图

I have run into similar issues using Celery in production, completely unrelated to Boto. 我在生产中使用Celery遇到了类似的问题,与Boto完全无关。 Although I do not have an explanation for the memory leak (this would take some serious code spelunking and profiling), I can offer a potential workaround if your goal is just to not run out of memory. 虽然我没有内存泄漏的解释(这需要一些严重的代码搜索和分析),但如果你的目标只是为了不耗尽内存,我可以提供一个潜在的解决方法。

Setting max tasks per child should allow you to constantly reclaim the memory as it is released by the killed process. 设置每个子项的最大任务应该允许您不断回收被杀死进程释放的内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM