AWS Lambda使用Python子进程并行调用CLI[马科斯]

2024-05-18 10:18:04 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我尝试使用AWS CLI使用子进程调用lambda函数,以便调用并行运行。我的函数的运行时间约为60秒,但我的CLI调用似乎并没有全部完成,而是以后的调用必须等待以前的调用完成。这会在我得到响应和我没有达到100个并发函数调用的目标之间造成很大的差距

以下是我的程序的简化版本:

#!/usr/bin/env python3

import ast
import datetime
import json
import random
import subprocess
import time

# Get the payload from exampleCLI.json
exp = json.load(open("./experiments/exampleCLI.json"))
payload = random.choice(exp['payloads'])
jsonString = str(json.dumps(payload))

# Define the lambda function to call.
cmd = ['aws', 'lambda', 'invoke', '--invocation-type', 'RequestResponse', '--function-name', 'multiCalcService', '--region', 'us-east-1', '--payload', jsonString, '/dev/stdout']

# Create the processes all at once.
calls = 100
procs = [ subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) for i in range(calls) ]

print("Threads created, waiting for responses.... Processes created: " + str(len([procs])))

# Get the responses.
for process in procs:
    o, e = process.communicate()
    jsonResponse = str(o.decode('ascii')).split('\n')[0][:-1]
    print(str(datetime.datetime.now()))

回复如下所示:

2019-06-27 14:26:29.717961
2019-06-27 14:26:29.718099
2019-06-27 14:26:29.718194
2019-06-27 14:26:30.072807
2019-06-27 14:26:30.072960
2019-06-27 14:26:30.073088
2019-06-27 14:26:30.073170
2019-06-27 14:26:30.073252
2019-06-27 14:26:30.289007
2019-06-27 14:26:30.289227
2019-06-27 14:26:30.289321
2019-06-27 14:27:27.229260 <-- Major gap
2019-06-27 14:27:27.229404
2019-06-27 14:27:27.687887
2019-06-27 14:27:27.688045
2019-06-27 14:27:27.688160
2019-06-27 14:27:30.918193
2019-06-27 14:27:30.918362
2019-06-27 14:27:30.918477
2019-06-27 14:27:30.918597
2019-06-27 14:27:30.918699
2019-06-27 14:27:30.918808
2019-06-27 14:27:30.918912
2019-06-27 14:27:30.919017
2019-06-27 14:27:30.919107
2019-06-27 14:27:30.919211
2019-06-27 14:27:30.919315
2019-06-27 14:27:30.919387
2019-06-27 14:27:30.919474
2019-06-27 14:27:30.919657
2019-06-27 14:27:30.919780
2019-06-27 14:27:30.919887
2019-06-27 14:27:30.920030
2019-06-27 14:27:30.920107
2019-06-27 14:27:30.920185
2019-06-27 14:27:30.920258
2019-06-27 14:27:30.920330
2019-06-27 14:28:29.887825 <- Major gap
2019-06-27 14:28:29.887976
2019-06-27 14:28:29.888115
2019-06-27 14:28:29.888213
2019-06-27 14:28:29.888286
2019-06-27 14:28:29.888431
2019-06-27 14:28:29.888558
2019-06-27 14:28:29.888646
2019-06-27 14:28:29.888722
2019-06-27 14:28:29.888798
2019-06-27 14:28:29.888877
2019-06-27 14:28:29.888952
2019-06-27 14:28:29.889084
2019-06-27 14:28:29.889195
2019-06-27 14:28:29.889282
2019-06-27 14:28:29.889433
2019-06-27 14:28:29.889569
2019-06-27 14:28:29.889667
2019-06-27 14:28:29.889746
2019-06-27 14:28:29.889876
2019-06-27 14:28:29.890000
2019-06-27 14:28:29.890096
2019-06-27 14:28:29.890179
2019-06-27 14:28:29.890265
2019-06-27 14:28:29.890343
2019-06-27 14:28:29.890419
2019-06-27 14:28:29.890497
2019-06-27 14:28:29.890603
2019-06-27 14:28:29.890691
2019-06-27 14:28:29.890821
2019-06-27 14:28:29.890926
2019-06-27 14:28:29.890999
2019-06-27 14:28:29.891072
2019-06-27 14:28:29.891152
2019-06-27 14:28:29.891233
2019-06-27 14:28:29.891308
2019-06-27 14:28:29.891382
2019-06-27 14:28:29.891456
2019-06-27 14:28:29.891533
2019-06-27 14:28:29.891607
2019-06-27 14:28:29.891681
2019-06-27 14:28:29.891768
2019-06-27 14:28:29.891877
2019-06-27 14:28:29.892031
2019-06-27 14:28:29.892165
2019-06-27 14:28:29.892252
2019-06-27 14:28:29.892330
2019-06-27 14:28:29.892403
2019-06-27 14:28:29.892479
2019-06-27 14:28:29.892554
2019-06-27 14:28:29.892631
2019-06-27 14:28:29.892707
2019-06-27 14:28:29.894837
2019-06-27 14:28:29.894992
2019-06-27 14:28:29.895173
2019-06-27 14:28:29.895292
2019-06-27 14:28:29.895412
2019-06-27 14:28:29.895540
2019-06-27 14:28:29.895635
2019-06-27 14:28:29.895722
2019-06-27 14:28:29.895800
2019-06-27 14:28:29.895878
2019-06-27 14:28:29.895959

正如我之前所说的,这些主要差距导致我无法达到所需的100个并发调用。我已确保多次运行此脚本以预热FaaS平台,但即使在多次迭代后仍能看到此行为

这个问题不是AWS Lambda独有的,当通过其CLI调用IBM或Google云函数时,也会遇到同样的行为。问题似乎出在MacOS上的Python子进程上

我的教授在运行Ubuntu的笔记本电脑上运行了这个程序,但没有遇到这些漏洞。我尝试在Ubuntu上使用VM运行Python程序,并看到了相同的行为。这让我很困惑,到底是什么导致了这个问题

任何能够解释为什么会发生这种情况的建议都将不胜感激


Tags: thelambda函数import程序awsjsonfor