繁体   English   中英

AWS Lambda Python datetime.now() 获取最新部署时间

[英]AWS Lambda Python datetime.now() getting the latest deployment time

疯狂的事情!
https://mli5b6535jtcvi235pbsklo4sq0cwajd.lambda-url.sa-east-1.on.aws/

代码只是:

import json
from datetime import datetime
now = datetime.now()

def lambda_handler(event, context):

   return {
        'statusCode': 200,
        'body': json.dumps(str(now))
    }

它返回2022-08-27 22:32:59.792352 ,但我重新部署了 function。

To return the current time the datetime.now() function call would need to be inside the lambda_handler function as this will be called each time your lambda function is invoked. 当使用第三方 SDK 或类似的 SDK 时,这可以为您带来优势,以避免在每次调用时重新创建它们,并且仅在启动运行时时才重新创建它们。

只有在部署新代码时启动 lambda 运行时或 lambda 缩放 ZC1C425268E68385D1AB5074C17A94F14 的运行时数量时,才会设置变量“now”。 有关 lambda 函数的生命周期的更多信息https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtime-environment.html#runtimes-lifecycle-ib

暂无
暂无

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

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