的一種方式,但我不相信這是最佳的:
多數民衆贊成由CloudWatch的觸發事件(比如每秒或每10秒,這取決於你的速度極限)一個lambda。其中調查SQS接收(至多)N條消息,然後用每條消息「扇出」到另一個Lambda函數。
一些僞代碼:
# Lambda 1 (schedule by CloudWatch Event/e.g. CRON)
def handle_cron(event, context):
# in order to get more messages, we might have to receive several times (loop)
for message in queue.receive_messages(MaxNumberOfMessages=10):
# Note: the Event InvocationType so we don't want to wait for the response!
lambda_client.invoke(FunctionName="foo", Payload=message.body, InvocationType='Event')
和
# Lambda 2 (triggered only by the invoke in Lambda 1)
def handle_message(event, context):
# handle message
pass
這是一個有趣的用例。你能否告訴我們爲什麼需要1秒的速率限制(即使在廣義上)?它可以通過Executor完成,該Executor每秒生成1個線程並處理恰好1個SQS輪詢,然後在不爲空的情況下處理失敗的隊列 - 但我仍然對它希望的位置感到好奇。謝謝! –
我們正在使用它來與第三方API進行通信,該API會限制我們使用他們的服務,每秒最多請求1個請求。 – bashaus