如何在python中重试钩子?

时间:2015-11-05 08:51:30

标签: python decorator retrypolicy

如果发生了一些Exception,我想重试我的方法,如下所示,也许用装饰器做这个更好?

class Processor(object):
    def __init__(self, username, password):
        self.username = username
        self.password = password
        self.user_info = UserInfo.objects.get(username=username)

    def get_inbox_page(self):
        try:
            # TODO process inbox page
            pass
        except NotLoginException:
            self.login()
            self.get_inbox_page()

    def get_downloaded_page(self):
            try:
            # TODO process downloaded page
            pass
        except NotLoginException:
            self.login()
            self.get_downloaded_page()

    def get_accept_msg_page(self):
        try:
            # TODO process accept message page
            pass
        except NotLoginException:
            self.login()
            self.get_accept_msg_page()

    # ... some other similar methods

    def login(self):
        session = self.login_proxy(self.username, self.password)
        self.user_info.session = session
        self.user_info.save()

0 个答案:

没有答案