跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • Python async/await 的协程 怎么取到返回值呢?
未分類
5 2 月 2021

Python async/await 的协程 怎么取到返回值呢?

Python async/await 的协程 怎么取到返回值呢?

資深大佬 : PPTX 8

async def SendHttpReq(url, method, params=None, data=None,                 headers=None,verify=None, cert=None, json_data=None):      async with httpx.AsyncClient() as client:         if (method == "GET"):             response = await client.get(url)     return response 

比如我外部函数想调用这个 SendHttpReq,取到 response 。这个不能直接像函数一样 resp = SendHttpReq()。

大佬有話說 (5)

  • 資深大佬 : Vegetable

    外部也需要 resp =await SendHttpReq()

  • 資深大佬 : towry

    “`py
    import asyncio

    resp = asyncio.ensure_future(SendHttpReq(‘url’, ‘GET’))
    “`

  • 資深大佬 : sss495088732

    0.0 题外话,httpx,aiohttp 这种异步 http 库都会有套接字溢出的问题.
    请见 requests 文档介绍:
    Requests 唯一的一个非转基因的 Python HTTP 库,人类可以安全享用。

    警告:非专业使用其他 HTTP 库会导致危险的副作用,包括:安全缺陷症、冗余代码症、重新发明轮子症、啃文档症、抑郁、头疼、甚至死亡。

  • 資深大佬 : julyclyde

    上面 await 和 ensure_future 的区别在哪方面呢

  • 資深大佬 : frostming

    @julyclyde ensure_future 并不能拿到结果,还是得 await 一下,所以按 1 的直接 await 就好

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具