簡體   English   中英

從 Slack 調用 Lambda function 時超時

[英]Timeout when calling Lambda function from Slack

我在這篇文章之后有與斜杠命令相關聯的代碼,並且斜杠命令正在工作。

如果延遲超過 3 秒出現超時錯誤,我該如何避免這種情況?

import json
from urllib import parse as urlparse
import base64
from functools import lru_cache
import math

@lru_cache(maxsize=60)
def isPrime(i):
    if (i in [2,3]):  # shortcut low primes
        return True
    else:
        if (i % 2 == 0 or i % 3 == 0):  # special since we go 3-> sqrt
            return False
        sqrt = int(math.sqrt(i) // 1)
        for s in range(3,sqrt+1,2):  # check odd vals, or all prior primes + new primes
            if (i % s == 0):
                return False
        return True

commands = {'isprime':isPrime,'prime':isPrime }   # map of command aliases

def lambda_handler(event, context):
    msg_map = dict(urlparse.parse_qsl(base64.b64decode(str(event['body'])).decode('ascii')))  # data comes b64 and also urlencoded name=value& pairs
    command = msg_map.get('command','err')  # will be /command name
    params = msg_map.get('text','err').split(" ")  # params ['isPrime','50']
    subcommand = params[0].lower()
    if (len(params) < 2):
        response = f'available subcommands: {list(commands.keys())} + 1 parameter'
    elif (subcommand in commands.keys()):
        response = f'{subcommand} needs an numeric param' if len(params) < 2 else f'{subcommand} = {commands[subcommand](int(float(params[1])))}'
    else:
        response = f'illegal sub command >{subcommand}<, commands available {list(commands.keys())}'

    # logging
    print (str(command) + ' ' + str(params) +' -> '+ response + ',original: '+ str(msg_map))

    return  {
        "response_type": "in_channel",
        "text": command + ' ' + " ".join(params),
        "attachments": [
            {
                "text": response
            }
        ]
    }

如何添加 5 分鍾等待響應。 Slack 在三秒后超時

failed with the error "operation_timeout"

更新

這與 Lambda 超時無關。 我已經增加了它,經過進一步研究,似乎有一個 3000 毫秒的硬限制,因為松弛強加的響應時間在這個問題的答案中有所描述

您可能遇到了 Lambda 超時。 AWS Lambda 運行您的代碼以響應事件,最長運行時間為 900 秒/15 分鍾。 此限制可以按照文檔中的說明進行配置,默認為 3 秒

解決方案:增加 Lambda 函數超時值。


更新

由於您已經能夠消除 Lambda 超時是一個問題,並指出這個問題說響應的硬限制為 3000 毫秒,所以我們遇到了一個不同的問題:

代碼很慢。

檢查素數是一個計算密集型過程,如果需要在短時間內完成,您可以花錢解決問題(CPU 馬力)。 In Lambda this means increasing the memory capacity of the function, because in Lambda CPU, RAM and Networking performance scale with the allocated memory.

一個 lambda function 和一個 Memory 設置為1280 MB應該給你大約一個完整的 vCPU,這是你的單線程實現的上限。 你可以試試下一個。

如果這不起作用,您需要更有效地實現isPrime算法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM