我正在设置一个 lambda 函数,它使用 asyncio 和 aiohttp 发出异步请求。即使代码在本地运行时工作正常,一旦我将其上传到 lambda,它就会返回:
"errorMessage": "A Future or coroutine is required"
我查看了已经打开的问题,例如 Boto3: A Future or coroutine is required和 TypeError: A Future or coroutine is required但无法使其正常工作。
我不确定为什么在我有定义的协程时返回错误消息
base_url = "https://www.reed.co.uk/api/1.0/jobs/"
url_list = [
"38012438",
"38012437",
"38012436"]
def lambda_handler(event, context):
client = boto3.client('s3',
aws_access_key_id="aws_access_key_id",
aws_secret_access_key="aws_secret_access_key")
async def fetch(session, url):
auth = aiohttp.BasicAuth(login='login', password='')
async with aiohttp.ClientSession(auth=auth) as session:
async with session.get(url) as response:
return await response.json()
async def fetch_all(urls, loop):
async with aiohttp.ClientSession(loop=loop) as session:
results = await asyncio.gather(*[fetch(session, base_url + url) for url in url_list], return_exceptions=True)
return results
loop = asyncio.get_event_loop()
htmls = loop.run_until_complete(fetch_all(urls, loop))
#I believe this is where the coroutine is
with open("/tmp/JOBS.json", "w") as f:
json.dump(htmls, f)
我只想将请求的组合内容上传到 json 文件。
我对自己有限的编码技能表示歉意,因为我对 python、lambda 等还不熟悉。
最佳答案
检查您的requirements.txt 文件。当我将 asyncio 添加到 requests.txt 时,我遇到了同样的错误。 AWS 似乎使用了特殊版本的 python。 Asyncio 是 python 的一部分。如果您单独安装它,则它无法在 AWS Lambda 中运行。
关于python - 类型错误 : A Future or coroutine is required with AWS lambda,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56220704/