简体   繁体   English

没有 aiohttp 的异步循环

[英]Async loop without aiohttp

I found library with operation on exchanges, i need make async or another method like async.我找到了对交易所进行操作的库,我需要 make async 或其他方法,如 async。 My code:我的代码:

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))

I need make this function really speed because it is really slow.我需要让这个 function 真正加速,因为它真的很慢。 Earlier i used async and aiohttp like:之前我使用过 async 和 aiohttp ,例如:

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()

I need something similar.我需要类似的东西。 Regards.问候。

I make this:我做这个:

import asyncio
import ccxt

tab_symbols = []

async def fetch(pair, client):
   async with client.binance().fetch_order_book(pair, 5) as response:
        return await print(response)

async def run(tab):
    tasks = []

    async with ccxt.binance() as client:
        for x in tab:
            task = asyncio.ensure_future(fetch(x, client))
            tasks.append(task)

       responses = await asyncio.gather(*tasks)
       print(responses)

if __name__ == '__main__':
    binance = ccxt.binance()

    for x in binance.load_markets():
        tab_symbols.append(x)
        print(x)
    loop = asyncio.get_event_loop()
    future = asyncio.ensure_future(run(tab_symbols))
    loop.run_until_complete(future)

But i have exception:但我有例外:

async with ccxt.binance() as client:
AttributeError: __aexit__

What is wrong?怎么了?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM