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

How to restart connection after connection failure (for whatever reason)? #3021

Closed
cowboyjj opened this issue Jun 3, 2018 · 4 comments
Closed
Assignees

Comments

@cowboyjj
Copy link

cowboyjj commented Jun 3, 2018

Hi, I don't know whether it's me doing it wrong. Basically, when I run the 'async-gdax-fetch-ticker-continuously.py' file provided by ccxt (accessing only one exchange, querying for only the price of one symbol, and setting 'enableRateLimit' to True), my connection never lasts for long.

The longest it lasts is five hours and more often it loses it earlier than that. I've tried Binance, huobipro, gdax, and they all failed for me after a while.

I get errors like:

RequestTimeout: huobipro GET https://api.huobipro.com/market/detail/merged?symbol=ethusdt

This is affecting me because I can't build a bigger program with other features if the connection is so unstable.

Can anybody gives suggestions? Is there a way to catch exception (what type of exception is it?)? Is there a way to re-start the connection or even the program when a connection failure is detected? Or am I simply doing anything wrong?

Many thanks

@kroitor
Copy link
Member

kroitor commented Jun 3, 2018

Have you tried implementing exception handling recommendations for RequestTimeout from the Manual / Error Handling section?

https://github.com/ccxt/ccxt/wiki/Manual#requesttimeout

@kroitor kroitor self-assigned this Jun 3, 2018
@cowboyjj
Copy link
Author

cowboyjj commented Jun 3, 2018

Hi kroitor, this may just be what I need!

I will explore the suggestions. It will take a few days as I will observe whatever fix works or not. If it works I will report back.

At the mean time, much appreciated if anybody who already has a skeleton code that works in this situation shares their work

@kroitor kroitor closed this as completed in f7eb169 Jun 3, 2018
@kroitor
Copy link
Member

kroitor commented Jun 3, 2018

# -*- coding: utf-8 -*-

import asyncio
import os
import sys
import ccxt.async as ccxt  # noqa: E402


async def main(exchange, symbol):
    while True:
        print('--------------------------------------------------------------')
        print(exchange.iso8601(exchange.milliseconds()), 'fetching', symbol, 'ticker from', exchange.name)
        # this can be any call instead of fetch_ticker, really
        try:
            ticker = await exchange.fetch_ticker(symbol)
            print(exchange.iso8601(exchange.milliseconds()), 'fetched', symbol, 'ticker from', exchange.name)
            print(ticker)
        except ccxt.RequestTimeout as e:
            print('[' + type(e).__name__ + ']')
            print(str(e)[0:200])
            # will retry (or add your own reaction here)
        except ccxt.DDoSProtection as e:
            print('[' + type(e).__name__ + ']')
            print(str(e.args)[0:200])
            # will retry (or add your reaction here)
        except ccxt.ExchangeNotAvailable as e:
            print('[' + type(e).__name__ + ']')
            print(str(e.args)[0:200])
            # will retry (or add your reaction here)
        except ccxt.ExchangeError as e:
            print('[' + type(e).__name__ + ']')
            print(str(e)[0:200])
            break  # won't retry, will break the loop

# you can set enableRateLimit = True to enable the built-in rate limiter
# this way you request rate will never hit the limit of an exchange
# the library will throttle your requests to avoid that

exchange = ccxt.gdax({
    'enableRateLimit': True,  # this option enables the built-in rate limiter
})

asyncio.get_event_loop().run_until_complete(main(exchange, 'LTC/USD'))

The updated script for fetching tickers that won't crash, and even if there's a RequestTimeout, it will repeat the call:

https://github.com/ccxt/ccxt/blob/master/examples/py/async-gdax-fetch-ticker-continuously.py

Hope this answers your question. You obviously need to learn more about basic exception handling in Python. There's a ton of tutorials for that on the internet.

@cowboyjj
Copy link
Author

cowboyjj commented Jun 4, 2018

Nice this is excellent. Giving it a go! Thanks Kroitor

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

No branches or pull requests

2 participants