我的python Cloud Function每秒引发约0.05个内存错误-每秒被调用约150次。我感到我的函数留下了内存残差,一旦它们处理了许多请求,就会导致其实例崩溃。您应该做什么或不应该做什么,以使函数实例在每次调用时都不会“占用更多的分配内存”?我已指向文档学习I should delete all temporary files,因为这是在内存中写的,但我认为我没有写任何东西。
我的函数代码可以总结如下。
由于我在Stackdriver中所做的这张图,我相信我的实例正在逐渐消耗所有可用的内存:
这是我的Cloud函数实例中的内存使用情况的热图,红色和黄色表示我的大多数函数实例都在使用此范围的内存。由于似乎出现了这种循环,我将其解释为实例内存的逐渐填充,直到它们崩溃并产生新实例为止。如果我提高分配给该函数的内存,则此周期仍然存在,它只是提高了该周期所遵循的内存使用上限。
请求中包含有助于在电子商务网站上实施跟踪的参数。现在,我复制了它,可能会有一个反模式,当我在其上进行迭代时修改form['products']
,但是我认为它与内存浪费没有关系吗?
from json import dumps
from datetime import datetime
from pytz import timezone
from google.cloud import storage
from google.cloud import pubsub
from google.cloud import error_reporting
from unidecode import unidecode
# this is done in global context because I only want to load the BOTS_LIST at
# cold start
PROJECT_ID = '...'
TOPIC_NAME = '...'
BUCKET_NAME = '...'
BOTS_PATH = '.../bots.txt'
gcs_client = storage.Client()
cf_bucket = gcs_client.bucket(BUCKET_NAME)
bots_blob = cf_bucket.blob(BOTS_PATH)
BOTS_LIST = bots_blob.download_as_string().decode('utf-8').split('\r\n')
del cf_bucket
del gcs_client
del bots_blob
err_client = error_reporting.Client()
def detect_nb_products(parameters):
'''
Detects number of products in the fields of the request.
'''
# ...
def remove_accents(d):
'''
Takes a dictionary and recursively transforms its strings into ASCII
encodable ones
'''
# ...
def safe_float_int(x):
'''
Custom converter to float / int
'''
# ...
def build_hit_id(d):
'''concatenate specific parameters from a dictionary'''
# ...
def cloud_function(request):
"""Actual Cloud Function"""
try:
time_received = datetime.now().timestamp()
# filtering bots
user_agent = request.headers.get('User-Agent')
if all([bot not in user_agent for bot in BOTS_LIST]):
form = request.form.to_dict()
# setting the products field
nb_prods = detect_nb_products(form.keys())
if nb_prods:
form['products'] = [{'product_name': form['product_name%d' % i],
'product_price': form['product_price%d' % i],
'product_id': form['product_id%d' % i],
'product_quantity': form['product_quantity%d' % i]}
for i in range(1, nb_prods + 1)]
useful_fields = [] # list of keys I'll keep from the form
unwanted = set(form.keys()) - set(useful_fields)
for key in unwanted:
del form[key]
# float conversion
if nb_prods:
for prod in form['products']:
prod['product_price'] = safe_float_int(
prod['product_price'])
# adding timestamp/hour/minute, user agent and date to the hit
form['time'] = int(time_received)
form['user_agent'] = user_agent
dt = datetime.fromtimestamp(time_received)
form['date'] = dt.strftime('%Y-%m-%d')
remove_accents(form)
friendly_names = {} # dict to translate the keys I originally
# receive to human friendly ones
new_form = {}
for key in form.keys():
if key in friendly_names.keys():
new_form[friendly_names[key]] = form[key]
else:
new_form[key] = form[key]
form = new_form
del new_form
# logging
print(form)
# setting up Pub/Sub
publisher = pubsub.PublisherClient()
topic_path = publisher.topic_path(PROJECT_ID, TOPIC_NAME)
# sending
hit_id = build_hit_id(form)
message_future = publisher.publish(topic_path,
dumps(form).encode('utf-8'),
time=str(int(time_received * 1000)),
hit_id=hit_id)
print(message_future.result())
return ('OK',
200,
{'Access-Control-Allow-Origin': '*'})
else:
# do nothing for bots
return ('OK',
200,
{'Access-Control-Allow-Origin': '*'})
except KeyError:
err_client.report_exception()
return ('err',
200,
{'Access-Control-Allow-Origin': '*'})
答案 0 :(得分:1)
您可以尝试一些方法(理论上的答案,我还没有玩过CF):
明确删除在bot处理路径上分配的临时变量,这些临时变量可能相互引用,从而防止内存垃圾收集器释放它们(请参见https://stackoverflow.com/a/33091796/4495081):nb_prods
,unwanted
,form
,new_form
,friendly_names
。
如果unwanted
始终相同,则改为全局。
删除form
,然后再将其分配给new_form
(保留旧的form
对象);同样,删除new_form
实际上不会节省太多,因为该对象仍然被form
引用。即更改:
form = new_form
del new_form
进入
del form
form = new_form
在发布主题之后并返回之前,显式调用内存垃圾收集器。我不确定这是否适用于CF或调用是否立即有效(例如,在GAE中不是,请参见When will memory get freed after completing the request on App Engine Backend Instances?)。这也可能会过大,并有可能损害CF的性能,请查看它是否/如何为您工作。
gc.collect()