所以我决定尝试自己通过一个小实验来解决这个问题。我使用 Python 2.7 创建了一个 Lambda 函数,具有128 MB 的 RAM、15 秒的超时并启用了主动跟踪。我修改了示例代码,在 import 语句之后添加了一个 10 秒的睡眠:
print "starting import"
import json
from time import sleep
sleep(10)
print "calling handler"
def lambda_handler(event, context):
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
由于 Lambda 开始变冷,我在 X 射线输出中看到了这一点:
我在 CloudWatch 日志中看到了这一点:
22:06:47 starting import
22:06:57 calling handler
22:06:58 START RequestId: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx Version: $LATEST
22:06:58 starting import
22:07:08 calling handler
22:07:08 END RequestId: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
22:07:08 REPORT RequestId: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx Duration: 10022.57 ms Billed Duration: 10100 ms Memory Size: 128 MB Max Memory Used: 19 MB
该函数实际上运行了TWICE。第一次休眠 10 秒后,它在调用处理程序方法时重新启动,基本上需要 20 秒才能完成执行,但会向我收费 10 秒。
我再次运行它,这次是热启动,我得到了这个:
X 射线输出(热启动):
CloudWatch 日志(热启动):
22:23:16 START RequestId: yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy Version: $LATEST
22:23:16 END RequestId: yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy
22:23:16 REPORT RequestId: yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy Duration: 6.97 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 29 MB
那里没什么可疑的。我将函数内存增加到 192 MB,将其保存并恢复为 128 MB,然后再次保存以确保它会再次冷启动并再次调用它。X-ray 的输出与之前相同,但 CloudWatch 日志有一些有趣的东西:
22:30:13 starting import
22:30:24 START RequestId: zzzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz Version: $LATEST
22:30:24 starting import
22:30:34 calling handler
22:30:34 END RequestId: zzzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz
22:30:34 REPORT RequestId: zzzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz Duration: 10010.85 ms Billed Duration: 10100 ms Memory Size: 128 MB Max Memory Used: 19 MB
似乎当我的代码处于休眠状态 10 秒时,Lambda 将其切断并重新启动。执行时间再次为 20 秒,但我被收取了 10 秒的费用。所以我想如果不是 1 个睡眠语句,而是添加 15 个一秒睡眠会怎样?
更新代码:
print "starting import"
import json
from time import sleep
for i in range(1, 16):
sleep(1)
print "completed {}th sleep".format(i)
print "calling handler"
def lambda_handler(event, context):
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
函数超时!
X射线输出:
CloudWatch 日志:
22:51:54 starting import
22:51:55 completed 1th sleep
22:51:56 completed 2th sleep
22:51:57 completed 3th sleep
22:51:58 completed 4th sleep
22:51:59 completed 5th sleep
22:52:00 completed 6th sleep
22:52:01 completed 7th sleep
22:52:02 completed 8th sleep
22:52:03 completed 9th sleep
22:52:04 START RequestId: 11111111-1111-1111-1111-111111111111 Version: $LATEST
22:52:04 starting import
22:52:05 completed 1th sleep
22:52:06 completed 2th sleep
22:52:07 completed 3th sleep
22:52:08 completed 4th sleep
22:52:09 completed 5th sleep
22:52:10 completed 6th sleep
22:52:11 completed 7th sleep
22:52:12 completed 8th sleep
22:52:13 completed 9th sleep
22:52:14 completed 10th sleep
22:52:15 completed 11th sleep
22:52:16 completed 12th sleep
22:52:17 completed 13th sleep
22:52:18 completed 14th sleep
22:52:19 END RequestId: 11111111-1111-1111-1111-111111111111
22:52:19 REPORT RequestId: 11111111-1111-1111-1111-111111111111 Duration: 15015.16 ms Billed Duration: 15000 ms Memory Size: 192 MB Max Memory Used: 19 MB
22:52:19
2019-03-29T22:52:19.621Z 11111111-1111-1111-1111-111111111111 Task timed out after 15.02 seconds
22:52:19 starting import
22:52:20 completed 1th sleep
22:52:21 completed 2th sleep
22:52:22 completed 3th sleep
22:52:23 completed 4th sleep
22:52:24 completed 5th sleep
22:52:25 completed 6th sleep
22:52:26 completed 7th sleep
22:52:27 completed 8th sleep
22:52:28 completed 9th sleep
22:52:29 completed 10th sleep
它实际上执行了 25.8 秒,但随后超时并向我收取了 15 秒的费用。在调用处理程序之前执行的代码运行了大约 9 秒,然后 Lambda 将其切断并重新启动该函数,但没有完成并最终在 25.8 秒后超时。如果我将 Lambda 超时时间增加到 16 秒,它会在 25.8 秒内完成执行(如 X-Ray 所示),并向我收取 15100 毫秒的费用。
所以这让我相信,如果在初始化后大约 9-10 秒内没有调用处理函数,Lambda 将重新启动该函数。那么如果代码初始化时间不到 10 秒呢?
更新代码:
print "starting import"
import json
from time import sleep
for i in range(1, 10):
sleep(1)
print "completed {}th sleep".format(i)
print "calling handler"
def lambda_handler(event, context):
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
我冷运行了这个函数大约 10 次,我的计费持续时间总是 100 毫秒。我什至将我的 lambda 超时更改为 1 秒,但它仍然成功执行!
X 射线输出:
CloudWatch 日志:
23:23:43 starting import
23:23:44 completed 1th sleep
23:23:45 completed 2th sleep
23:23:46 completed 3th sleep
23:23:47 completed 4th sleep
23:23:48 completed 5th sleep
23:23:49 completed 6th sleep
23:23:50 completed 7th sleep
23:23:51 completed 8th sleep
23:23:52 completed 9th sleep
23:23:52 calling handler
23:23:52 START RequestId: 22222222-2222-2222-2222-222222222222 Version: $LATEST
23:23:52 END RequestId: 22222222-2222-2222-2222-222222222222
23:23:52 REPORT RequestId: 22222222-2222-2222-2222-222222222222 Duration: 0.73 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 44 MB
正如Steve HOUEL正确指出的那样,这让我相信Lambda 不会向您收取初始化代码(例如导入内容)所需的时间,只要它在大约 9 秒内完成即可。但是,如果花费的时间比这更长,Lambda 会重新启动您的函数,并假设您设置了足够大的超时时间,函数执行实际上需要 10 秒 + 常规冷启动执行时间,但您仍然只需为冷启动执行时间付费,而无需增加了 10 秒。