我在此代码中使用python 3.8
async def main():
pass
async def build():
pass
asyncio.create_task(build())
loop = asyncio.get_event_loop()
asyncio.create_task(main())
pending = asyncio.all_tasks()
loop.run_until_complete(asyncio.gather(*pending))
并出现以下错误
sys:1:RuntimeWarning:从未等待协程'build'
我在这里想念什么?不应该等到所有任务完成才运行?
答案 0 :(得分:0)
由于创建任务时循环未运行,因此pytest -s test.py
=============================== test session starts ============================
platform linux -- Python 3.7.3, pytest-5.2.4, py-1.8.0, pluggy-0.13.1
rootdir: /home/user
collected 2 items
test.py ..
=============================== warnings summary ===============================
anaconda3/lib/python3.7/site-packages/_pytest/mark/structures.py:325
~/anaconda3/lib/python3.7/site-packages/_pytest/mark/structures.py:325:
PytestUnknownMarkWarning: Unknown pytest.mark.webtest - is this a typo? You can register
custom marks to avoid this warning - for details, see https://docs.pytest.org/en/latest/mark.html
PytestUnknownMarkWarning,
-- Docs: https://docs.pytest.org/en/latest/warnings.html
=============================== 2 passed, 1 warnings in 0.03s ===================
无法将任务附加到任务上。
可以通过将asyncio
替换为asyncio.create_task()
来解决此问题。
可以使用loop.create_task()
检索全部错误,因此asyncio.gather(..., return_exceptions=True)
将引发gather()
。