我有一个 AWS Lambda 函数:
import boto3
import json
import concurrent.futures
from stats import Timer
client = boto3.client('lambda')
def do_lambda():
timer = Timer()
response = client.invoke(
FunctionName = 'arn:aws:lambda:us-east-1:497857710590:function:wherewasit-search',
InvocationType = 'RequestResponse',
Payload = json.dumps({'keys': 1}))
payload = json.loads(json.load(response['Payload'])['body'])
print(f"Response after {timer.stop()}ms")
def do_work(n_threads):
timer = Timer()
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = []
for i in range(n_threads):
futures.append(executor.submit(do_lambda))
for future in concurrent.futures.as_completed(futures):
pass
print(f"Joined all: {timer.stop()}ms")
def lambda_handler(event, context):
do_work(9)
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
输出:
Response after 1236ms
Response after 1242ms
Response after 1251ms
Response after 1272ms
Response after 1322ms
Response after 1359ms
Response after 1126ms
Response after 1170ms
Response after 1246ms
Joined all: 2496ms
假设所有线程最多在 1.4 秒内完成,那么在所有线程连接之前又花费了多少秒?