python – 捕获greenlets中引发的异常

我正试图捕捉greenlets中提出的异常.根据this tutorial,不幸的是,在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()

解决方法:

Greenlets将保存为greenlet或异常生成的函数的结果(如果已经引发).您可以使用get方法获取结果或重新引发异常.
由于你似乎正在改变基于任何错误的greenlet的状态,所以同步方法最终会看起来像:

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()
上一篇:开发基于Django和Websocket的堡垒机


下一篇:123 Python程序中的线程操作-协程