python thread dump

一直希望python也有类似java dump的功能,上网找了一段代码可以实现类似效果:



def stacktraces():
code = []
import sys
import traceback

for threadId, stack in sys._current_frames().items():
code.append("# ThreadID: %s" % threadId)
for filename, lineno, name, line in traceback.extract_stack(stack):
code.append('File: "%s", line %d, in %s' % (filename, lineno, name))
if line:
code.append(" %s" % (line.strip()))

return "<br>".join(code)



<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span style="color: #408080; font-style: italic">

# ThreadID: 47988642959488<br>File: "build/bdist.linux-x86_64/egg/gevent/greenlet.py", line 390, in run<br> result = self._run(*self.args, **self.kwargs)<br>File: "/duitang/dist/sys/python/lib/python2.7/site-packages/gunicorn-0.14.6-py2.7.egg/gunicorn/workers/async.py", line 44, in handle<br> self.handle_request(req, client, addr)<br>File: "/duitang/dist/sys/python/lib/python2.7/site-packages/gunicorn-0.14.6-py2.7.egg/gunicorn/workers/ggevent.py", line 88, in handle_request<br> super(GeventWorker, self).handle_request(*args)<br>File: "/duitang/dist/sys/python/lib/python2.7/site-packages/gunicorn-0.14.6-py2.7.egg/gunicorn/workers/async.py", line 78, in handle_request<br> respiter = self.wsgi(environ, resp.start_response)<br>File: "/duitang/dist/sys/python/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 273, in __call__<br> response = self.get_response(request)<br>File: "/duitang/dist/sys/python/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response<br> response = callback(request, *callback_args, **callback_kwargs)<br>File: "/duitang/dist/sys/python/lib/python2.7/site-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view<br> return view_func(request, *args, **kwargs)<br>File: "/duitang/dist/sys/python/lib/python2.7/site-packages/django/contrib/admin/views/decorators.py", line 19, in _checklogin<br> return view_func(request, *args, **kwargs)<br>File: "/data1/duitang/dist/app/test/7199/duitang/analytic/views.py", line 878, in dump<br> stack = cf.stacktraces()<br>File: "/data1/duitang/dist/app/test/7199/duitang/cf.py", line 835, in stacktraces<br> for filename, lineno, name, line in traceback.extract_stack(stack):

</span> </pre></div>



[img]http://dl.iteye.com/upload/attachment/0072/6323/9d7f9781-fb2b-3691-aea0-558b56586b63.png[/img]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值