0

Web サービスへの呼び出しを処理する Python をコーディングしています。

def calculate(self):
    market_supply_price = self.__price_to_pay_current_market_supply()
    market_supply_price_usd = market_supply_price.get('usd')
    market_supply_price_eur = market_supply_price.get('eur')
    amount = '%.8f' % ((self.euro - ((self.euro*self.__tax_to_apply())+self.__extra_tax_to_apply())) / market_supply_price_eur)
    return {'usd': [market_supply_price_usd, amount], 'eur': [market_supply_price_eur, amount]}

Web サービスへの呼び出しは次の行にあります。

market_supply_price = self.__price_to_pay_current_market_supply()

このプライベート メソッドは、Web サービスに対してさまざまな呼び出しを行い、結果を返します。ここでの問題は、この Web サービスが頻繁に失敗することです。呼び出しの 1 つが失敗した場合、たとえば 10 分間待ってから再試行する方法を実装する必要があります。10 分後に再び失敗した場合は、30 分待ってから再試行し、30 分後に再び失敗した場合は、 60分待って…

calculate() メソッドでこのようなものを実装するための最良の方法は何ですか?

私はこのようなものを実装しましたが、それは間違っているように見え、行うべき方法ではありません。

def calculate(self):
    try:
        market_supply_price = self.__price_to_pay_current_market_supply()
    except:
        pass
        try:
            time.sleep(600)
            market_supply_price = self.__price_to_pay_current_market_supply()
        except:
            pass
            try:
                time.sleep(600)
                market_supply_price = self.__price_to_pay_current_market_supply()
            except:
                pass
                try:
                    time.sleep(1200)
                    market_supply_price = self.__price_to_pay_current_market_supply()
                except:
                    sys.exit(1)
    market_supply_price_usd = market_supply_price.get('usd')
    market_supply_price_eur = market_supply_price.get('eur')
    amount = '%.8f' % ((self.euro - ((self.euro*self.__tax_to_apply())+self.__extra_tax_to_apply())) / market_supply_price_eur)
    return {'usd': [market_supply_price_usd, amount], 'eur': [market_supply_price_eur, amount]}

これを正しい方法で行う方法の手がかりはありますか?

よろしくお願いします、

4

1 に答える 1

0

ループは、このタイプのものに適しています。タイムアウトを指定する例を次に示します。

def calculate(self):
    for timeout in (600, 600, 1200):
        try:
            market_supply_price = self.__price_to_pay_current_market_supply()
            break
        except: # BAD! Catch specific exceptions
            sleep(timeout)
    else:
        print "operation failed"
        sys.exit(1)

    market_supply_price_usd = market_supply_price.get('usd')
    market_supply_price_eur = market_supply_price.get('eur')
    etc...
于 2014-01-30T20:31:51.463 に答える