Python 无aiohttp的异步循环
我在Exchange上找到了操作库,我需要使用async或其他类似async的方法。我的代码:Python 无aiohttp的异步循环,python,asynchronous,Python,Asynchronous,我在Exchange上找到了操作库,我需要使用async或其他类似async的方法。我的代码: 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)) 我需要使这个功能真正的速度,因为它真的很慢。前面我使用了异步和
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))
我需要使这个功能真正的速度,因为它真的很慢。前面我使用了异步和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()
我需要类似的东西。问候。我谨此致辞:
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)
但我有一个例外:
async with ccxt.binance() as client:
AttributeError: __aexit__
怎么了?如果请求速度慢,您可以使用ThreadPoolExecutor并行执行多个请求,但该库似乎已经支持asyncio:Ok。但是如何使用请求创建多个选项卡呢?有很多资源可用于使用ThreadPoolExecutor,如和异步请求,如。你需要表明你已经尽力尝试了,如果你在过程中遇到问题,发布你的代码,人们会帮助你;阅读我在上面评论中链接的例子;更具体地说,使用
import ccxt.async\u支持作为ccxt