Rest API asyncio gets stuck while running

888 Views Asked by At

Can anyone help me with Rest API asynchronous? I used Asyncio for 4 requests to FTX server. If I run the code, it prints 'balance','position','chart1','chart2' and stops working. It doesn't incur any errors but it seems to get stuck somewhere, probably during the requests. How can I fix this issue?

import ccxt.async_support as ccxt
import asyncio
import FTXdata

api_key = "api_key "
secret  = "api_secret "

ftx = ccxt.ftx(config={
    'apiKey': api_key, 
    'secret': secret,
    })

symbols = {'DOT-PERP':'DOT-PERP'}
       

    #1    
async def balance():
    print('balance')
    balance = await ftx.fetch_balance()
    await asyncio.sleep(1)

    #2
async def position(exchange, symbol):
    print('position')
    position= await exchange.fetch_positions(symbol)         
    await asyncio.sleep(1)

    #3
async def dataframe(exchange, symbol):
    print('chart1')    
    data = await exchange.fetch_ohlcv(
        symbol=symbol,               
        timeframe='5m', 
        since=None, 
        limit=100)    
    await asyncio.sleep(1)

    #4
async def dataframe1h(exchange, symbol):
    print('chart2') 
    data1h = await exchange.fetch_ohlcv(
        symbol=symbol,               
        timeframe='1h', 
        since=None, 
        limit=30)
    await asyncio.sleep(1)

async def main(exchange,symbol):
    task1 = asyncio.create_task(balance())
    task2 = asyncio.create_task(position(exchange, symbol))
    task3 = asyncio.create_task(dataframe(exchange, symbol))
    task4 = asyncio.create_task(dataframe1h(exchange, symbol))
    await asyncio.wait([task1,task2,task3,task4])
    print('finish')

op_mode = False

while True: 
    for symbol in symbols: 
    asyncio.run(main(ftx,symbol))
1

There are 1 best solutions below

0
On

I've edited your code a bit and it works fine now. I've added some prints of the variables received so you can see the data.

Remember that asyncio works asynchronously so the order the data is printed in the console might not always be the same way it is ordered in your code since asyncio is waiting to receive the data before it can do anything with it.

import ccxt.async_support as ccxt
import asyncio

api_key = '...'
secret  = '...'

ftx = ccxt.ftx(config={
    'apiKey': api_key, 
    'secret': secret,
    })

symbols = {'DOT-PERP':'DOT-PERP'}
       
    #1    
async def balance():
    balance = await ftx.fetch_balance()
    print('balance')
    print(balance)
    await asyncio.sleep(1)

    #2
async def position(exchange, symbol):
    position = await exchange.fetch_positions(symbol)
    print('position')
    print(position)
    await asyncio.sleep(1)

    #3
async def dataframe(exchange, symbol):
    data = await exchange.fetch_ohlcv(
        symbol=symbol,               
        timeframe='5m', 
        since=None, 
        limit=100)
    print('chart1')
    print(data)
    await asyncio.sleep(1)

    #4
async def dataframe1h(exchange, symbol): 
    data1h = await exchange.fetch_ohlcv(
        symbol=symbol,               
        timeframe='1h', 
        since=None, 
        limit=30)
    print('chart2')
    print(data1h)
    await asyncio.sleep(1)

async def main(exchange,symbol):
    task1 = asyncio.create_task(balance())
    task2 = asyncio.create_task(position(exchange, symbol))
    task3 = asyncio.create_task(dataframe(exchange, symbol))
    task4 = asyncio.create_task(dataframe1h(exchange, symbol))
    await asyncio.wait([task1,task2,task3,task4])
    print('finish')

op_mode = False

while True: 
    for symbol in symbols: 
        asyncio.run(main(ftx,symbol))

One thing to note is that you are not working with the REST API here but with websockets which works a lot better with asyncio. I will amend your question for greater accuracy.