我有一个AWS lambda函数,需要以异步方式(激发并忘记)调用该函数,并以非阻塞方式将其返回。
loop = asyncio.get_event_loop()
async def f(payload):
lambda_client = boto3.client('lambda')
response = lambda_client.invoke(
FunctionName='FUNC_NAME',
InvocationType='RequestResponse',
LogType='Tail',
Payload=payload,
Qualifier='$LATEST'
)
response_body = response['Payload']
response_str = response_body.read().decode('utf-8')
response_dict = eval(response_str)
return response_dict
async def g():
payload = json.dumps({
"test_bucket": "MY_BUCKET",
"test_key": "my_test_key.csv",
"testpred_bucket": "MY_BUCKET",
"testpred_key": "my_test_key_new.csv",
"problem": "APROBLEM"
})
# Pause here and come back to g() when f() is ready
r = await f(payload)
print(r)
这可行,但这实际上并没有达到fire and forget
的目的。我了解需要使用asyncio.ensure_future
的方式,但是如果执行asyncio.ensure_future(f(payload))
,如何捕获函数f
的返回值。我是python异步的新手,目前尚不清楚。
有人可以建议吗?
答案 0 :(得分:0)
loop = asyncio.get_event_loop()
if loop.is_closed():
loop = asyncio.new_event_loop()
#The perform_traces method i do all the post method
task = loop.create_task(perform_traces(payloads, message, contact_centre))
unique_match, error = loop.run_until_complete(task)
loop.close()
在perform_trace
方法中,这就是我使用会话等待的方式
future_dds_responses = []
async with aiohttp.ClientSession() as session:
for payload in payloads:
future_dds_responses.append(dds_async_trace(session, payload, contact_centre))
dds_responses, pending = await asyncio.wait(future_dds_responses)
在dds_async_trace
中,这就是我使用aiohttp.ClientSession会话完成帖子的方式
async with session.post(pds_url,
data=populated_template_payload,
headers=PDS_HEADERS,
ssl=ssl_context) as response:
status_code = response.status
答案 1 :(得分:0)
我能够用asyncio.ensure_future
import boto3
import json
from flask import jsonify
import signal
import sys
import asyncio
import aiohttp
import json
def run():
loop = asyncio.get_event_loop()
loop.run_until_complete(run_job())
async def run_job():
asyncio.ensure_future(launch_lambda()) # fire and forget async_foo()
print('waiting for future ...')
async def launch_lambda():
payload = json.dumps({
"test_bucket": "my_bucket",
"test_key": "my_test_key",
"testpred_bucket": "my_bucket",
"testpred_key": "my_pred_key",
"problem": "APROBLEM"
})
result = await get_lambda_response(payload)
print(result)
async def get_lambda_response(payload):
lambda_client = boto3.client('lambda')
response = lambda_client.invoke(
FunctionName='FUNC_NAME',
InvocationType='RequestResponse',
LogType='Tail',
Payload=payload,
Qualifier='$LATEST'
)
response_body = response['Payload']
response_str = response_body.read().decode('utf-8')
response_dict = eval(response_str)
return response_dict
run()