mirror of
https://github.com/discountry/ritmex-bot.git
synced 2026-09-11 09:18:08 +00:00
commit
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
- [Async Tickers From Many Exchanges At Once](./examples/py/)
|
||||
|
||||
|
||||
```python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import asyncio
|
||||
import ccxt
|
||||
import ccxt.async_support as ccxta # noqa: E402
|
||||
import time
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
||||
|
||||
def sync_client(exchange):
|
||||
client = getattr(ccxt, exchange)()
|
||||
tickers = client.fetch_tickers()
|
||||
return tickers
|
||||
|
||||
|
||||
async def async_client(exchange):
|
||||
client = getattr(ccxta, exchange)()
|
||||
tickers = await client.fetch_tickers()
|
||||
await client.close()
|
||||
return tickers
|
||||
|
||||
|
||||
async def multi_tickers(exchanges):
|
||||
input_coroutines = [async_client(exchange) for exchange in exchanges]
|
||||
tickers = await asyncio.gather(*input_coroutines, return_exceptions=True)
|
||||
return tickers
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
# Consider review request rate limit in the methods you call
|
||||
exchanges = ["coinex", "bittrex", "bitfinex", "poloniex", "hitbtc"]
|
||||
|
||||
tic = time.time()
|
||||
a = asyncio.run(multi_tickers(exchanges))
|
||||
print("async call spend:", time.time() - tic)
|
||||
|
||||
time.sleep(1)
|
||||
|
||||
tic = time.time()
|
||||
a = [sync_client(exchange) for exchange in exchanges]
|
||||
print("sync call spend:", time.time() - tic)
|
||||
|
||||
```
|
||||
Reference in New Issue
Block a user