Witam. Potrzebuję zrobić asynchroniczną lub podobną funkcję, która szybko wykona polecenia. Przykładowy kod:
import ccxt
binance = ccxt.binance()
for pair in binance.load_markets(): // example: pair = BTC/USDT or ETH/BTC ...
print(binance.fetch_order_book(pair, 5))
Potrzebuję tą funkcję napisać asynchronicznie. Kiedyś używałem async i aiohttp i wyglądało to tak:
async def main():
async with aiohttp.ClientSession() as session:
await asyncio.wait([fetch(self, session, url) for url in urls])
print('Done')
async def fetch(session: aiohttp.ClientSession, url: str):
async with session.get(url) as content:
content = await content.json()
print(f"Requested: {url}")
def run():
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(main())
loop.run_until_complete(asyncio.sleep(2.0))
finally:
loop.close()