【问题标题】:How to deal with 'await' outside async function?如何处理异步函数之外的“等待”?
【发布时间】:2021-09-23 01:44:35
【问题描述】:

下面的函数正在调用 create_presigned_url 但我在等待时遇到错误。

 def getPreSignedURL(request: Request, file: UploadFile = File(...) ):
       resp = await create_presigned_url(request,file)
       return resp

这是我要调用的异步函数

async def create_presigned_url(bucket_name, object_name, expiration=3600):
---
return response

【问题讨论】:

  • 请提供错误。
  • 您不能从同步方法调用异步方法。欲了解更多信息stackoverflow.com/questions/55647753/…
  • 您不能如上所述从常规函数调用异步函数。您需要使用 asyncio。
  • @tomarv 在哪里使用这个异步?

标签: python async-await fastapi


【解决方案1】:

希望这会有所帮助。从常规函数调用异步函数的示例:

import asyncio

loop = asyncio.get_event_loop()


async def demo(name):
    return f"hello {name}"


def main():
    result = loop.run_until_complete(demo("world"))
    print(result)


if __name__ == '__main__':
    main()

【讨论】:

  • 是的,我做到了,我在函数声明之前缺少异步,这已经解决了问题。感谢您的帮助
  • @sakshisinghal 如果你同意这个答案,你介意投票吗?
猜你喜欢
  • 2021-09-18
  • 2020-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-02
  • 2021-01-13
  • 2021-11-01
相关资源
最近更新 更多