4

我正在尝试捕捉greenlets中引发的异常。根据本教程,不幸的是'Greenlet中提出的异常,留在Greenlet中'。在下面的代码中,我有一个同步方法,它会生成爬虫绿叶。那些爬虫会引发 HTTPError 异常,我需要在同步方法的主体中捕获它。基于异常,在sync方法中我会设置Account对象的相应状态。知道如何在同步方法中捕获 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()
4

1 回答 1

2

Greenlets 将保存为 greenlet 或异常生成的函数的结果,如果引发了一个。您可以使用该方法获取结果或重新引发异常get。由于您似乎正在根据任何错误的greenlets更改状态,因此同步方法最终看起来像:

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()
于 2013-04-21T15:40:00.853 回答