python - 执行看起来与 asyncio 同步

python - the execution looks like synchronous with asyncio

Api 调用可以通过异步调用来加速。 所以我想我可以通过使用 asyncio 来加速我的程序。通过显示结果,执行看起来像是同步的。所以我完全糊涂了。我做错了什么?

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


async def catch_up_aggtrades(client, symbols):
    start = time.time()   
    for symbol in symbols:
        await get_historical_aggtrades(client, symbol)
    end = time.time()
    print(end-start)


async def get_historical_aggtrades(client, symbol):
    async for trade in client.aggregate_trade_iter(symbol, '5 minutes ago UTC'):
        print("symbol {} -> {}".format(symbol, trade))


async def main():
    client = await AsyncClient.create()
    symbols = ['BTCUSDT', 'ETHUSDT', 'BNBUSDT']
    await catch_up_aggtrades(client, symbols)
    while True:
       await asyncio.sleep(20, loop=loop)


if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

试试这个:

async def catch_up_aggtrades(client, symbols):
    start = time.time()   
    await asyncio.gather(*[get_historical_aggtrades(client, symbol) for symbol in symbols])
    end = time.time()
    print(end-start)