如何在 gevent 中捕获回溯

发布于 2025-01-05 09:02:43 字数 242 浏览 1 评论 0原文

我生成了一个 Greenlet 并将其链接到一个可调用对象。一段时间后,Greenlet 因异常而失败。链接的可调用对象被调用。太棒了!

问题是这样的:

正如您所期望的,异常的回溯出现在我的控制台上。但我想在链接的可调用对象中使用回溯进行操作。如何访问链接的可调用对象中的回溯?

(我的第一反应是使用traceback.extract_stack(),但事实证明它为链接的可调用本身提供了回溯,而不是为异常提供了回溯。)

I've spawned a Greenlet and linked it to a callable. Some time later, the Greenlet fails with an Exception. The linked callable gets called. That's all great!

Here's the issue:

The traceback for the Exception appears on my console, as you'd expect. But I want do things with that traceback within the linked callable. How do I get access to that traceback within the linked callable?

(My first instinct was to use traceback.extract_stack(), but it turns out that provides a traceback for the linked callable itself and not for the Exception.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧人九事 2025-01-12 09:02:43

当 Greenlet 死亡时,特意不保存回溯。如果它被保存,它将使许多预计将被删除的对象保持活动状态,这在对象管理某些资源(打开文件或套接字)时尤其重要。

如果你想保存回溯,你必须自己做。

The traceback is intentionally not saved when the Greenlet dies. If it was saved, it would keep a lot of objects alive that are expected to be deleted, which matters especially if the object manages some resource (open file or socket).

If you want to save the traceback you have to do it yourself.

夏雨凉 2025-01-12 09:02:43

作为使用 Greenlet.link_exception 的 Stephen Diehl 解决方案的替代方案。

import traceback

import gevent

def job():
    raise Exception('ooops')

def on_exception(greenlet):
    try:
        greenlet.get()
    except Exception:
        err = traceback.format_exc()
        # Do something with `err`

g = gevent.spawn(job)
g.link_exception(on_exception)

As an alternative to Stephen Diehl's solution using Greenlet.link_exception.

import traceback

import gevent

def job():
    raise Exception('ooops')

def on_exception(greenlet):
    try:
        greenlet.get()
    except Exception:
        err = traceback.format_exc()
        # Do something with `err`

g = gevent.spawn(job)
g.link_exception(on_exception)
一梦等七年七年为一梦 2025-01-12 09:02:43

Greenlet 对象应该有一个 exception 属性,您可以查看该属性:

http://www.gevent.org/gevent.html#gevent.Greenlet.exception

The Greenlet object should have an exception property that you can look at:

http://www.gevent.org/gevent.html#gevent.Greenlet.exception

优雅的叶子 2025-01-12 09:02:43

只需确保获取 Greenlet 的 Exception 值并将其扔到 Greenlet 之外,例如 get 返回返回的值或引发内部异常。

import traceback
import gevent

def fail():
    return 0/0

gl = gevent.spawn(fail)

try:
    gl.get()
except Exception as e:
    stack_trace = traceback.format_exc() # here's your stacktrace

应该给你你需要的东西。

Just make sure you grab the exception value of the Greenlet and throw it outside the Greenlet, for example get returns either the value returned or raises the internal exception.

import traceback
import gevent

def fail():
    return 0/0

gl = gevent.spawn(fail)

try:
    gl.get()
except Exception as e:
    stack_trace = traceback.format_exc() # here's your stacktrace

Should give you what you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文