2
while true:
    ticker = binance.fetch_ticker("BTC/USDT")
    current_price = ticker['last']
function_A(current_price)

I have a while loop that keeps running to check the current price of Bitcoin every second. Then I also have a function that takes current_price as an input.

However, occasionally, I am getting

"requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))"

So I'm trying to use try, except to make a retry mechanism whenever this error comes up. I've tried this:

while true:
    try_count = 10
    while try_count > 0:
        try:
            ticker = binance.fetch_ticker("BTC/USDT")
            current_price = ticker['last']
            try_count = 0
            break
        except (requests.ConnectionError, requests.ReadTimeout) as error:
            print(error, " detected. Trying again...")
            try_count -= 1
    function_A(current_price)

The problem is that if I do this, current_price ends up being undefined when I plug it in as an input in function_A on the last line. How can I fix this mechanism?

2 Answers 2

1

Defining current_price in a scope outside the second while loop can prevent the problem with current_price sometimes being undefined when calling function_A.

while True:
    current_price = None
    try_count = 10
    while try_count > 0:
        try:
            ticker = binance.fetch_ticker("BTC/USDT")
            current_price = ticker['last']
            try_count = 0
            break
        except (requests.ConnectionError, requests.ReadTimeout) as error:
            print(error, " detected. Trying again...")
            try_count -= 1
    if current_price is not None:
        function_A(current_price) 
Sign up to request clarification or add additional context in comments.

3 Comments

So far, it's working without an error but I'll need to check in later. Thanks for your help! If I had another input for function_A named "amount," would I just add amount = None after the second line? Then say, "if current_price is not None and amount is not None:" for the last if-loop?
Yes. That would work
Thanks again! I am curious though how this works. What is the point of saying current_price = None in the beginning?
0

you may want to explore the backoff library which helps to retry your function. https://pypi.org/project/backoff/

you can just add a backoff decorator on fetch_ticker function and it should retries when it hits RequestException error.

the code should look something like

@backoff.on_exception(backoff.expo, requests.exceptions.RequestException)
def fetch_ticker(ticker):
    # binance.fetch_ticker

1 Comment

This looks promising but I'm definitely going to need to search some examples of it being used. Thanks for the pointer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.