首页 > 解决方案 > 为什么我有时在使用 asyncio.run 时从未等待协程

问题描述

此错误很少见,但仍然存在。当同时发送两个或多个请求时会发生这种情况。

我尝试这样做:

adapter = OperatorCreator.create(
    title=operator.slug,
    api_key=operator.api_discount[0]
)

terminals = asyncio.run(
    adapter.find_terminals(locality=locality), debug=True
)

return terminal

适配器:

async def find_terminals(self, locality: Locality, session: aiohttp.ClientSession = None) -> Mapping[str, Terminal]:
    if 'dpd_terminals' in cache:
        terminal_list = await sync_to_async(cache.get)('dpd_terminals')
    else:
        terminal_list = await self.add_terminals_from_request_to_cache()
    terminals = terminal_list[str(locality.kladr)]
    return terminals

然后有时我得到:

django_1 | /app/project/apps/geo/services.py:383:RuntimeWarning:从未等待协程“CdekAdapter.find_terminals”

django_1 | adapter.find_terminals(locality=locality)

django_1 | RuntimeWarning: 启用 tracemalloc 以获取对象分配回溯

我究竟做错了什么?

标签: pythondjangopython-asynciocoroutine

解决方案


推荐阅读