You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS Lambda热启动下部分调用耗时突增问题排查(非重复提问)

注意

此问题曾被错误标记为重复提问,本问题聚焦AWS Lambda热启动耗时突增,而非通用Python代码计时(代码已实现毫秒级精准计时)。若认为问题重复,请先留言说明,问题关闭后恢复难度较大。

AWS Lambda函数代码
import json
import time
import pickle

def ms_now():
    return int(time.time_ns() / 1000000)

class Timer():
    def __init__(self):
        self.start = ms_now()

    def stop(self):
        return ms_now() - self.start

timer = Timer()

from punctuators.models import PunctCapSegModelONNX
model_name = "pcs_en"
model_sentences = PunctCapSegModelONNX.from_pretrained(model_name)

with open('model_embeddings.pkl', 'rb') as file:
    model_embeddings = pickle.load(file)

cold_start = True
init_time = timer.stop()
print("Time to initialize:", init_time, flush=True)

def segment_text(texts):
    sentences = model_sentences.infer(texts)
    sentences = [
        [(s, len(model_embeddings.tokenizer.encode(s))) for s in el]
         for el in sentences]
    return sentences

def get_embeddings(texts):
    return model_embeddings.encode(texts)

def compute(body):
    command = body['command']
    
    if command == 'ping':
        return 'success'

    texts = body['texts']

    if command == 'embeddings':
        result = get_embeddings(texts)
        return [el.tolist() for el in result]
    
    if command == 'sentences':
        return segment_text(texts)
    
    assert(False)

def lambda_handler(event, context):
    global cold_start
    global init_time
    
    stats = {'cold_start': cold_start, 'init_time': init_time}
    cold_start = False
    init_time = 0

    stats['started'] = ms_now()
    result = compute(event['body'])
    stats['finished'] = ms_now()
    return {
        'statusCode': 200,
        'headers': {
            'Content-Type': 'application/json'
        },
        'body': {'result': result, 'stats': stats}
    }
部署与测试情况
  • 函数以Docker镜像形式部署,包含所有依赖包与模型,无需在线下载。
  • 通过另一个函数并行调用该Lambda函数15次,响应中包含以下统计信息:
    • cold_start:标识是否为冷启动
    • init_time:初始化耗时(冷启动时init_time>6000ms,热启动时init_time==0)
    • started/finished:执行阶段的时间戳,用于计算执行耗时
异常现象

热启动状态下,首次并行调用的执行耗时有时会从正常的约300ms突增至900ms甚至更长,且command == 'embeddings'和command == 'sentences'两种场景均会出现。

疑问

热启动状态下,哪些因素会导致Lambda函数耗时远超正常水平?


内容的提问来源于stack exchange,提问作者AlwaysLearning

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 16:42:27