首页 > 解决方案 > RuntimeWarning:从未等待协程“current_time”返回浮点(anyio.current_time())

问题描述

蟒蛇 3.8.8 httpx 0.18.2

这是我下面的代码。我正在使用 httpx&asyncio 进行测试。代码非常简单,与示例相同,但发生了错误。

headers = {"Content-type":"text/html", "charset":"UTF-8"}
async def gethtml(url):  
    async with httpx.AsyncClient(headers=headers, verify=False) as client:
        r = await client.get(url) 
        return r.status_code
url = 'http://www.google.com'
asyncio.run(gethtml(url))

当我运行它时,会出现问题。

RuntimeWarning: coroutine 'current_time' was never awaited
  return float(anyio.current_time())
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Traceback (most recent call last):
  File "/Users/yilu/opt/anaconda3/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3437, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-12f16719e1ee>", line 1, in <module>
    runfile('/Users/yilu/Documents/7k/aaa.py', wdir='/Users/yilu/Documents/7k/')
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/yilu/Documents/7k/aaa.py", line 53, in <module>
    asyncio.run(gethtml(url))   
  File "/Users/yilu/opt/anaconda3/lib/python3.8/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/Users/yilu/opt/anaconda3/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "/Users/yilu/Documents/7k/aaa.py", line 17, in gethtml
    r = await client.get(url)    
  File "/Users/yilu/opt/anaconda3/lib/python3.8/site-packages/httpx/_client.py", line 1722, in get
    return await self.request(
  File "/Users/yilu/opt/anaconda3/lib/python3.8/site-packages/httpx/_client.py", line 1481, in request
    response = await self.send(
  File "/Users/yilu/opt/anaconda3/lib/python3.8/site-packages/httpx/_client.py", line 1568, in send
    response = await self._send_handling_auth(
  File "/Users/yilu/opt/anaconda3/lib/python3.8/site-packages/httpx/_client.py", line 1604, in _send_handling_auth
    response = await self._send_handling_redirects(
  File "/Users/yilu/opt/anaconda3/lib/python3.8/site-packages/httpx/_client.py", line 1640, in _send_handling_redirects
    response = await self._send_single_request(request, timeout)
  File "/Users/yilu/opt/anaconda3/lib/python3.8/site-packages/httpx/_client.py", line 1681, in _send_single_request
    ) = await transport.handle_async_request(
  File "/Users/yilu/opt/anaconda3/lib/python3.8/site-packages/httpx/_transports/default.py", line 283, in handle_async_request
    ) = await self._pool.handle_async_request(
  File "/Users/yilu/opt/anaconda3/lib/python3.8/site-packages/httpcore/_async/http_proxy.py", line 102, in handle_async_request
    await self._keepalive_sweep()
  File "/Users/yilu/opt/anaconda3/lib/python3.8/site-packages/httpcore/_async/connection_pool.py", line 303, in _keepalive_sweep
    now = await self._backend.time()
  File "/Users/yilu/opt/anaconda3/lib/python3.8/site-packages/httpcore/_backends/auto.py", line 64, in time
    return await self.backend.time()
  File "/Users/yilu/opt/anaconda3/lib/python3.8/site-packages/httpcore/_backends/anyio.py", line 198, in time
    return float(anyio.current_time())
TypeError: float() argument must be a string or a number, not 'coroutine'

当我之前用python 3.7运行它时,这是正常的。但现在,我应付不来。你能帮我吗?谢谢

标签: pythonpython-asynciohttpx

解决方案


当我将 httpx 版本降低到 0.17.1 时,这个问题得到了解决。所以我认为这可能是httpx上的错误


推荐阅读