Dav*_*nez 5 python amazon-web-services aws-lambda
我有一个 lambda,我需要它正常关闭并登录到外部系统。在查看了有关此事的一些文献后,我使用线程得出了以下解决方案:
def lambda_handler(event, context):
threshold_millis = 10 * 1000 # leave when there are only 10 seconds left
que = queue.Queue()
t = threading.Thread(target=lambda q, ev: q.put(do_work(ev)), args=(que, event))
t.daemon = True
t.start()
while True:
if context.get_remaining_time_in_millis() < threshold_millis:
# Do some logging notifying the timeout
return {
"isBase64Encoded": False,
"statusCode": 408,
"headers": {'Content-Type': "application/json"},
"body": "Request timed out"
}
elif not t.isAlive():
response = que.get()
return response
time.sleep(1)
Run Code Online (Sandbox Code Playgroud)
虽然它有效,但我想知道:有没有比这个更好的实践来优雅地处理 AWS Lambda 中的超时?
观看get_remaining_time_in_millis
是 lambda 中抢占超时的最佳/推荐方式;没有调用任何特殊事件来让您知道即将超时。
在不了解具体情况的情况下,您可以通过查看收到错误所需的时间来推断客户端超时。不过,我更希望您的解决方案让 lambda 明确地表达出来。
归档时间: |
|
查看次数: |
1131 次 |
最近记录: |