gpt4 book ai didi

python - 捕获在 greenlet 中引发的异常

转载 作者:太空宇宙 更新时间:2023-11-03 11:32:14 24 4
gpt4 key购买 nike

我正在 try catch greenlet 中引发的异常。根据this tutorial ,不幸的是“在 Greenlet 中引发异常,留在 Greenlet 中”。在下面的代码中,我有一个生成爬行 greenlet 的同步方法。那些爬行 greenlets 引发了 HTTPError 异常,我需要在 sync 方法的主体中捕获它。基于异常,在 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()

最佳答案

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()

关于python - 捕获在 greenlet 中引发的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15961518/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com