I want to use a semaphore with a gather() to limit api calls. I think I have to use create_task() but I obtain a runtime error: "RuntimeError: await wasn't used with future". How can I fix it?
Here is the code:
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())