重复 API 调用缓存响应,所以我需要完全杀死它并重试

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

本质上,我有一组条件 运行 这个:

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

这使用 API,主要利用 MechanicalSoup 在网站上填写一两个表格。由于某种原因,该操作将在 运行ning 后失败,并将继续 运行(如 while True 所说),直到我强制退出程序。

我假设某些东西正在被缓存,因为没有合乎逻辑的理由它会失败数百次。

我怎样才能让它完全杀死它正在发生的一切并从头开始重试?

谢谢

编辑:更多代码

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 是 InvestopediaAPI 的主要模块,我用它来 运行 在 investopedia 的模拟交易员上进行买卖。如果我需要提供其中的一些代码 API 我可以去找它的源代码。

如pkisztelinski所说,你要求程序在出现异常时继续下一阶段的迭代,所以如果异常不断发生,可能会导致无限循环。

但是,如果您想知道发生的异常,您可以这样做

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