I have this code associated with slash command followed this article and slash commands are working.
If there a delay of more than 3 seconds there is a timeout error, how can I avoid this?
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
}
]
}
How to add 5 mins wait for the response. Slack is giving timeout after three seconds
failed with the error "operation_timeout"
This is not related to a Lambda timeout. I've increased it and upon further research it seems that there is a hard limit of 3000 ms for the response time imposed by slack as described in the answer to this question
You're probably running into a Lambda timeout. AWS Lambda runs your code in response to an event and for a maximum of 900 seconds / 15 minutes. This limit can be configured as described in the docs and is 3 seconds by default .
Solution: Increase the Lambda functions timeout value.
Since you have been able to eliminate the Lambda timeout being an issue and pointed to this question which says there is a hard limit of 3000ms for responses, we've got a different problem:
The code is to slow.
Checking for prime numbers is a compute intensive process and if it needs to be completed within a short period of time, you can throw money at the problem (CPU horsepower). In Lambda this means increasing the memory capacity of the function, because in Lambda CPU, RAM and Networking performance scale with the allocated memory.
A lambda function with a Memory Setting of 1280 MB
should give you roughly a whole vCPU, which is sort of the upper limit for your single threaded implementation. You could try that next.
If that doesn't work, you need a more efficient implementation of the isPrime
algorithm.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.