I'm having trouble understanding the mechanism of my code. I want to retrieve the realtime price data of a crypto from an exchange and print it every second, while creating a retry mechanism that catches an exception when it occurs.
while True:
try_cnt = 10
while try_cnt > 0:
try:
price = get_current_price(symbol) # function imported from elsewhere
print(price)
time.sleep(1)
try_cnt = 0
break
except Exception as e:
print(e)
try_cnt -= 1
This is how I have it set up but I'm having trouble follow the logic. This is how I see it as of now. After the code has been run:
Price is retrieved
try_cnt is set to 0 and the second while loop is broken
The next second, try_cnt is reset to 10 and price is retrieved
try_cnt is set to 0 again and the second while loop is broken
repeat of this process
Am I understanding this correctly?
EDIT: the reason why I have a while loop outside is that I have other things that are going on at specific times besides the part I mentioned here. The part that I mentioned is supposed to be running all the time every second. For example,
while True:
if now.hour == 0 and now.minute == 0 and now.second == 0:
A = functionA(x,y)
try_cnt = 10
while try_cnt > 0:
try:
price = get_current_price(symbol) # function imported from elsewhere
print(price)
time.sleep(1)
try_cnt = 0
break
except Exception as e:
print(e)
try_cnt -= 1
I think that you are understanding everything just fine, but the code itself is a little silly and redundant. For example, setting try_cnt to 0 and then following that with break is pointless - both lines do the same thing. And unless there is some significance to the variable try_cnt that is not present within the code you posted, its existence seems pretty much useless to me. Here's code that does the exact same thing as the code above, but without the redundancies:
while True:
try:
price = get_current_price(symbol)
print(price)
time.sleep(1)
except Exception as e:
print(e)
In response to your edit, the while True loop encompassing everything makes a lot more sense. However, this means that your code isn't doing what you want it to do. The outer while loop ensures that even if try_cnt reaches 0, try_cnt will just be reset to 10 on the next pass and it will be as if no Exceptions ever occurred (besides the ten error messages in the console). If you want that inner while loop to stop executing when ten Exceptions occur, you need to place the try_cnt = 10 statement outside of both while loops like so:
try_cnt = 10
while True:
if now.hour == 0 and now.minute == 0 and now.second == 0:
A = functionA(x,y)
while try_cnt > 0:
try:
price = get_current_price(symbol)
print(price)
time.sleep(1)
try_cnt = 10
break
except Exception as e:
print(e)
try_cnt -= 1
Related
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?
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)
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
I'm getting the following error when I run my code:
I'm guessing the root of this error is when I execute the fetch_ticker() function.
I have two python scripts: main.py and functions.py (includes all the functions)
I've read somewhere else that strategically adding a time.sleep(0.01) can solve the issue.
Would it be wise to add time.sleep(0.01) before any line that requests something through BinanceUS API?
For example,
if now.hour == 0 and now.minute == 0 and (0 <= now.second < 10):
time.sleep(0.01)
target = calc_target(binanceUS, symbol)
time.sleep(0.01)
balance = binanceUS.fetch_balance()
time.sleep(0.01)
usd = balance['total']['USD']
op_mode = True
time.sleep(10)
Though I'm not sure if this will solve the issue...or I also thought about creating an error-handling code for all the functions that I use, but I'm not sure how I would go about it when the function includes one or more if-statements. For example,
def enter_position(exchange, symbol, cur_price, target, amount, position):
try_cnt = 3
while try_cnt > 0:
if cur_price > target:
try:
position['type'] = 'long'
position['amount'] = amount
exchange.create_market_buy_order(symbol=symbol, amount=amount)
except Exception as e:
print("Connection error, trying again...")
try_cnt -= 1
time.sleep(1)
else:
exchange.create_market_buy_order(symbol=symbol, amount=amount)
Not sure if this will work, though.
I'm very confused about how to go around this error. In fact, I'm not even sure what the error means. Would any of the two solutions look plausible?
In the case I am looping into a list, where for value msg_list = 0 it will execute action(0), usr). This action can fail for a determined user, I should choose aother user if it happens, perform all the actions related to the user
How can I do to repeat action[0] if it fails?
for msg in range(len(msg_list)):
# in this case msg = 0
usr = select_random_user()
multiple_actions_for(usr) # This are lots of code lines I don't want to repeat!!
try:
action(msg, usr)
more_actions(usr.related_stuff)
except Exception as e:
go_back_to(msg =0 instead of looping into msg=1) # this is what I want to do
How can I do to get that? Repeat the loop for msg = i instead of passing to msg = i + 1?
Put your code into the endless while-loop with exiting from it if try was successful:
for msg in range(len(msg_list)):
while True:
usr = select_random_user()
multiple_actions_for(usr)
try:
action(msg, usr)
more_actions(usr.related_stuff)
except Exception as e:
continue
else:
break
It really depends. Is it okay to go back to the beginning of the loop? If so, you can call "continue", which stops the current iteration, and restarts the loop. Otherwise, I don't think there's something similar to a goto in Python, no. It's a very structured language.
Try using while loop instead of for loop. The idea is as shown bellow:
bool still_looping = True
msg = 0
while still_looping:
usr = select_random_user()
multiple_actions_for(usr)
try:
action(msg, usr)
more_actions(usr.related_stuff)
if (msg < len (msg_list)):
msg += 1
except Exception as e:
# Do whatever you want here. msg was not incremented
if (msg == len(msg_list))
still_looping = False #verify if this was the last execution
So there's this website that posts something I want to buy at a random time of day for a limited amount of time and I want to write something to send a message to my phone when a new url is posted to that webpage.
I planned on doing this by counting the number of links on the page (since it's rarely updated) and checking it every 5 minutes against what it was 5 minutes before that, then 5 minutes later check it against what it was 10 minutes before that, 5 minutes later check what it was 15 minutes before that... and if it's greater than what it originally was, send a message to my phone. Here's what I have so far:
class url_alert:
url = ''
def link_count(self):
notifyy=True
while notifyy:
try:
page = urllib.request.urlopen(self.url)
soup = bs(page, "lxml")
links=[]
for link in soup.findAll('a'):
links.append(link.get('href'))
notifyy=False
print('found', int(len(links)), 'links')
except:
print('Stop making so many requests')
time.sleep(60*5)
return len(links)
def phone(self):
self= phone
phone.message = client.messages.create(to="", from_="",body="")
print('notified')
def looper(self):
first_count = self.link_count()
print('outside while')
noty = True
while noty:
try:
second_count = self.link_count()
print('before compare')
if second_count == first_count:
self.phone()
noty = False
except:
print('not quite...')
time.sleep(60)
alert = url_alert()
alert.looper()
As a test, I decided to set the if statement that determines whether or not to send a message as equal but the loop kept on running. Am I calling the functions within the looper function the right way?
It looks like you need to eliminate the try block, as it is now, if self.phone() takes an exception you will never leave the loop
def looper(self):
first_count = self.link_count()
while True:
if first_count != self.link_count():
self.phone()
break
time.sleep(60)
Cont = 1
while Cont == 1:
try:
while Cont == 1:
counter = counter + 0.1
counter = round(counter, 1)
print(counter)
time.sleep(0.1)
if counter == crashNumber:
Cont = 0
except KeyboardInterrupt:
Multiplier = counter
Here the counter will continue to count up unitl it reaches the crashNumber, when Ctrl + C is pressed, it will take the number that the counter is at and use it for the Multiplier to be used later.
However I only want to give the user the chance to press this once, then it is disabled. Is there any way that this can be done?
The KeyboardInterrupt exception will be thrown whether you want it or not: the solution, then, is to deal with the exception in different ways in your except block. My chosen implementation will use a simple boolean value that starts as True and is set to False on the first interruption:
import time
allow_interrupt = True
while True:
try:
time.sleep(1)
print ('...')
except KeyboardInterrupt:
if allow_interrupt:
print ('interrupted!')
allow_interrupt = False
Let me know if this addresses your use case.