相关疑难解决方法(0)

python asyncio - 运行时错误:await 未与 future 一起使用

我想使用带有 Gather() 的信号量来限制 api 调用。我想我必须使用 create_task() 但我收到运行时错误:“RuntimeError:await 未与 future 一起使用”。我该如何修复它?

这是代码:

import asyncio
# pip install git+https://github.com/sammchardy/python-binance.git@00dc9a978590e79d4aa02e6c75106a3632990e8d
from binance import AsyncClient


async def catch_up_aggtrades(client, symbols):
    tasks = asyncio.create_task(get_historical_aggtrades(client, symbol) for symbol in symbols)
    sem = asyncio.Semaphore(1)
    async with sem:
        await asyncio.gather(*tasks)


async def get_historical_aggtrades(client, symbol):
    async for trade in client.aggregate_trade_iter(symbol, '1 day ago UTC'):
        print(f"symbol {symbol}")


async def main():
    client = await AsyncClient.create()
    symbols = ['BTCUSDT', 'ETHUSDT', 'BNBUSDT']
    await catch_up_aggtrades(client, symbols)


if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
Run Code Online (Sandbox Code Playgroud)

python semaphore limit python-asyncio

9
推荐指数
1
解决办法
1万
查看次数

标签 统计

limit ×1

python ×1

python-asyncio ×1

semaphore ×1