python - 类型错误 : A Future or coroutine is required with AWS lambda

标签 python python-asyncio

我正在设置一个 lambda 函数,它使用 asyncio 和 aiohttp 发出异步请求。即使代码在本地运行时工作正常,一旦我将其上传到 lambda,它就会返回:

"errorMessage": "A Future or coroutine is required"

我查看了已经打开的问题,例如 Boto3: A Future or coroutine is requiredTypeError: 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/

相关文章:

python - 协程外的Aiohttp ClientSession

python - 我能以某种方式与子进程共享一个异步队列吗?

python - asyncio create_task 永远运行

python在扫描mp3元数据后删除不需要的字符

python - 使用 Python heapq.merge 对大文件进行排序

python - 与 NA 左连接

Python OpenCv 段错误

python - 使用 asyncio 协程并行运行函数?

python - 如何阻止我的 websocket 连接关闭?

python - 使用多维键屏蔽数据框