繁体   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