从Python Gevent输出中删除stackTrace



我是python的新手,并在升高异常时尝试删除/修剪gevent stacktrace输出。我在某个地方阅读了可以通过使用异步来实现它的地方,但是我看来我不知道如何使用它。

这是我开始并迭代的一个示例,使其与我要故障排除的真实代码相似,但是当我尝试将my_decor添加到工作()时,我被卡在了最后一个阶段。对解决此问题的任何帮助将不胜感激。

from gevent.event import AsyncResult
import gevent
from functools import wraps

def my_decor(k, *args, **kwargs):
    @wraps(k)
    def wrapper(*args, **kwargs):
        r = AsyncResult()
        try:
            value = k()
        except Exception as e:
            r.set_exception(e)
        else:
            r.set(value)
        return r.exception or r.value
    result = gevent.spawn(wrapper, k)
    return result

def f():
    def foo():
        if True:
                raise Exception('tttttttt')
    return foo

def p():
    def bar():
        if True:
                raise Exception('ppppppppppppp')
    return bar

@my_decor
def work():
    foo1 = gevent.spawn(f())
    bar1 = gevent.spawn(p())
    gevent.joinall([foo1, bar1])
    return foo1.get() or bar1.get()

找到答案,认为这可能对那些有相同问题的人有帮助。

from gevent.event import AsyncResult
import gevent
from functools import wraps

def my_decor(k):
    @wraps(k)
    def wrapper(*args, **kwargs):
        r = AsyncResult()
        try:
            value = k(*args, **kwargs)
        except Exception as e:
            r.set_exception(e)
        else:
            r.set(value)
        return r.exception or r.value
    return wrapper

def f(msg):
    @my_decor
    def foo():
        if True:
            raise Exception('tttttttt %s' % msg)
#           print('test')
    return foo

def p(msg):
    @my_decor
    def bar():
        if True:
            raise Exception('ppppppppppppp %s', msg)
    return bar

def work():
    test = "test"
    seti = "set"
    foo1 = gevent.spawn(f(test))  # returns a function that coroutine uses
    bar1 = gevent.spawn(p(seti))
    gevent.joinall([foo1, bar1])
    return foo1.get() or bar1.get()

res = work()
print res

相关内容

  • 没有找到相关文章

最新更新