捕获greenlets中引发的异常

时间:2013-04-12 01:14:39

标签: python exception exception-handling gevent greenlets

我正试图捕捉greenlets中提出的异常。根据{{​​3}},不幸的是,在Greenlet中提出的例外情况,留在Greenlet内部。在下面的代码中,我有一个同步方法,它产生爬行greenlets。那些爬行greenlets引发HTTPError异常,我需要在同步方法体中捕获。基于异常,在sync方法中,我将设置Account对象的适当状态。任何想法我怎么能在sync方法中捕获greenlets中引发的异常才能实现呢?先感谢您!

# this is standalone helper method
def crawl(item):
    try:
        item.refresh_children(False) # THROWS HTTPError exception
        greenlets = [gevent.spawn_link_exception(crawl, child) for child in item.children]
        gevent.joinall(greenlets)
    except HTTPError, e:
        print e.message
        raise e
    except (JSONDecodeError, InvalidCredentialsException) as e:
        print e.message
        raise e

# this is instance method of the Account class 
def sync(self):
    "Sync search index with data from source"
    try:
        greenlets = [gevent.spawn_link_exception(crawl, item) for item in self.get_navigation()]
        gevent.joinall(greenlets)
        self.date_synced = datetime.datetime.now()
        self.save()
    except HTTPError, e:
        if e.status_code == 401:
            self.status = 'revoked'
        else:
            self.status = 'error'
        self.save()
    except LinkedFailed, e:
        print e.message
        exception_name = e.message.split()[-1]
        if exception_name in ['HTTPError', 'JSONDecodeError']:
            self.status = 'error'
            self.save()
        elif exception_name == 'InvalidCredentialsException':
            self.status = 'revoked'
            self.save()

1 个答案:

答案 0 :(得分:2)

Greenlets将保留为greenlet或异常生成的函数的结果(如果有的话)。您可以使用get方法获取结果或重新引发异常。 由于您似乎正在根据任何错误更改状态,因此同步方法最终会显示如下内容:

def sync(self):
    "Sync search index with data from source"
    greenlets = [gevent.spawn_link_exception(crawl, item) for item in self.get_navigation()]
    gevent.joinall(greenlets)
    try:
        results = [greenlet.get() for greenlet in greenlets]
    except HTTPError, e:
        if e.status_code == 401:
            self.status = 'revoked'
        else:
            self.status = 'error'
        self.save()
    except LinkedFailed, e:
        print e.message
        exception_name = e.message.split()[-1]
        if exception_name in ['HTTPError', 'JSONDecodeError']:
            self.status = 'error'
            self.save()
        elif exception_name == 'InvalidCredentialsException':
            self.status = 'revoked'
            self.save()

    self.date_synced = datetime.datetime.now()
    self.save()