Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unclosed client session and ddos Error for bitfinex #704

Closed
alpaykoray opened this issue Nov 28, 2017 · 3 comments
Closed

Unclosed client session and ddos Error for bitfinex #704

alpaykoray opened this issue Nov 28, 2017 · 3 comments
Assignees
Labels

Comments

@alpaykoray
Copy link

alpaykoray commented Nov 28, 2017

Hi,

First, thanks for this amazing work you have done. Second my problem is about below code. I constantly get "Unclosed client session client_session: <aiohttp.client.ClientSession object at 0x00000274BF7DAB38>" and Ddos error. I also tried to pu time.sleep(8) o get rid of it but didnt work. Is there any way to make it work efficiently? appreciate your help on this.

timeout = time.time() + 60*60*8

async def bitfinex():
    bitfinex = ccxt.bitfinex()
    return await bitfinex.fetch_ticker()
 
while True:
    if time.time() > timeout:
        break
    try:
      
        bitfinexratiolast = float(asyncio.get_event_loop().run_until_complete(ccxt.bitfinex().fetch_ticker('ETH/BTC'))['last'])

       bitfinexbtctrylast = float(asyncio.get_event_loop().run_until_complete(ccxt.bitfinex().fetch_ticker('BTC/USD'))['last'])

    except ccxt.DDoSProtection:
        print('ddos')
        continue
    
print(bitfinexratiolast)
print(bitfinexbtctrylast)

@kroitor kroitor self-assigned this Nov 28, 2017
@kroitor
Copy link
Member

kroitor commented Nov 28, 2017

You don't need to do ccxt.bitfinex() in every line. I suggest you learn more basic Python. Focus on working with classes and objects instances.

This code should work:

import asyncio
import ccxt.async as ccxt

async def run():
    bitfinex = ccxt.bitfinex({'enableRateLimit': True})
    while True:
        try:
            bitfinexratio = await bitfinex.fetch_ticker('ETH/BTC')
            bitfinexbtctry = await bitfinex.fetch_ticker('BTC/USD')
            print('----------------------------------------')
            datetimestring = bitfinex.iso8601(bitfinex.milliseconds())
            print(datetimestring, bitfinexratio['last'], bitfinexbtctry['last'])
        except Exception as e:
            print(type(e).__name__ + ' ' + str(e))

asyncio.get_event_loop().run_until_complete(run())

More:

@kroitor kroitor closed this as completed Nov 28, 2017
@alpaykoray
Copy link
Author

Thanks a lot! now it is working i will look at basic Python courses as well (y)

@kroitor
Copy link
Member

kroitor commented Nov 28, 2017

However, this one is much better:

import asyncio
import ccxt.async as ccxt

async def run():
    bitfinex = ccxt.bitfinex({'enableRateLimit': True})
    while True:
        try:
            tickers = await bitfinex.fetch_tickers()
            bitfinexratio = tickers['ETH/BTC']
            bitfinexbtctry = tickers['BTC/USD']
            print('----------------------------------------')
            datetimestring = bitfinex.iso8601(bitfinex.milliseconds())
            print(datetimestring, bitfinexratio['last'], bitfinexbtctry['last'])
        except Exception as e:
            print(type(e).__name__ + ' ' + str(e))

asyncio.get_event_loop().run_until_complete(run())

I'd recommend to fetch all tickers in one go like shown above if you need more than one of them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants