Search code examples
pythonloopswhile-loopmechanicalsoup

Repeating API call caches response so I need to fully kill it and try again


Essentially, I have a set of conditions that run this:

while True:
            try:
                print "trying to buy a"
                buy(ashr ,tickers[0], client)                  
            except:
                continue
            break

This uses an API that utilizes MechanicalSoup mostly to fill out a form or two on a website. For some reason, the action will fail after running and will continue to run (like the while True says) until I force quit the program.

I'm assuming that something is being cached because there's no logical reason it would fail hundreds of times over.

How can I get it to completely kill whatever it has going on and retry it from the ground up?

Thanks

EDIT: More code

def buy(shares, ticker, client):
    client.trade(ticker,ita.Action.buy, shares)

ashr = int(200/ita.get_quote(tickers[0]))

client = ita.Account("example.un", "example.pw")

ita is the main module of the InvestopediaAPI that I'm using to run buys and sells on investopedia's paper trader. If I need to give some of the code from that API I can go find it's source code.


Solution

  • as suggested by pkisztelinski, you asked the program to continue the next phase of the iteration when there is an exception, so if exception keeps occuring it may lead to an infinite loop.

    however, if you wanted to know the exception that occurred, you could do this

    while True:
                try:
                    print "trying to buy a"
                    buy(ashr ,tickers[0], client)                  
                except Exception as e:
                    print('Error occured '+str(e))
                    break