stone2083

pystack--python stack trace--让python打印线程栈信息

pystack: python stack trace. 类似java中的jstack功能.
使用方式:
1. https://pypi.python.org/pypi/pdbx/0.3.0 下载, 或者直接通过easyinstall安装
2. python scripts中, import pdbx; pdbx.enable_pystack(); 开启pystack功能
3. kill -30 pid , 就可以打印stack信息了. 
如:
"CP Server Thread-10" tid=4564467712
    at self.__bootstrap_inner()(threading.py:525)
    at self.run()(threading.py:552)
    at conn = self.server.requests.get()(__init__.py:1367)
    at self.not_empty.wait()(Queue.py:168)
    at waiter.acquire()(threading.py:244)

"CP Server Thread-9" tid=4560261120
    at self.__bootstrap_inner()(threading.py:525)
    at self.run()(threading.py:552)
    at conn = self.server.requests.get()(__init__.py:1367)
    at self.not_empty.wait()(Queue.py:168)
    at waiter.acquire()(threading.py:244)

"CP Server Thread-1" tid=4526608384
    at self.__bootstrap_inner()(threading.py:525)
    at self.run()(threading.py:552)
    at conn = self.server.requests.get()(__init__.py:1367)
    at self.not_empty.wait()(Queue.py:168)
    at waiter.acquire()(threading.py:244)

"CP Server Thread-7" tid=4551847936
    at self.__bootstrap_inner()(threading.py:525)
    at self.run()(threading.py:552)
    at conn = self.server.requests.get()(__init__.py:1367)
    at self.not_empty.wait()(Queue.py:168)
    at waiter.acquire()(threading.py:244)

"CP Server Thread-4" tid=4539228160
    at self.__bootstrap_inner()(threading.py:525)
    at self.run()(threading.py:552)
    at conn = self.server.requests.get()(__init__.py:1367)
    at self.not_empty.wait()(Queue.py:168)
    at waiter.acquire()(threading.py:244)

"CP Server Thread-2" tid=4530814976
    at self.__bootstrap_inner()(threading.py:525)
    at self.run()(threading.py:552)
    at conn = self.server.requests.get()(__init__.py:1367)
    at self.not_empty.wait()(Queue.py:168)
    at waiter.acquire()(threading.py:244)

"MainThread" tid=140735286018432
    at app.run()(raspctl.py:173)
    at return wsgi.runwsgi(self.wsgifunc(*middleware))(application.py:313)
    at return httpserver.runsimple(func, validip(listget(sys.argv, 1, '')))(wsgi.py:54)
    at server.start()(httpserver.py:157)
    at self.tick()(__init__.py:1765)
    at s, addr = self.socket.accept()(__init__.py:1800)
    at sock, addr = self._sock.accept()(socket.py:202)
    at pystack()(pdbx.py:181)
    at for filename, lineno, _, line in traceback.extract_stack(stack):(pdbx.py:169)

"CP Server Thread-5" tid=4543434752
    at self.__bootstrap_inner()(threading.py:525)
    at self.run()(threading.py:552)
    at conn = self.server.requests.get()(__init__.py:1367)
    at self.not_empty.wait()(Queue.py:168)
    at waiter.acquire()(threading.py:244)

"CP Server Thread-8" tid=4556054528
    at self.__bootstrap_inner()(threading.py:525)
    at self.run()(threading.py:552)
    at conn = self.server.requests.get()(__init__.py:1367)
    at self.not_empty.wait()(Queue.py:168)
    at waiter.acquire()(threading.py:244)

"CP Server Thread-3" tid=4535021568
    at self.__bootstrap_inner()(threading.py:525)
    at self.run()(threading.py:552)
    at conn = self.server.requests.get()(__init__.py:1367)
    at self.not_empty.wait()(Queue.py:168)
    at waiter.acquire()(threading.py:244)

"CP Server Thread-6" tid=4547641344
    at self.__bootstrap_inner()(threading.py:525)
    at self.run()(threading.py:552)
    at conn = self.server.requests.get()(__init__.py:1367)
    at self.not_empty.wait()(Queue.py:168)
    at waiter.acquire()(threading.py:244)


核心代码:
# pystack
def pystack():
    for tid, stack in sys._current_frames().items():
        info = []
        t = _get_thread(tid)
        info.append('"%s" tid=%d' % (t.name, tid))
        for filename, lineno, _, line in traceback.extract_stack(stack):
            info.append('    at %s(%s:%d)' % (line, filename[filename.rfind('/') + 1:], lineno))
        print '\r\n'.join(info)
        print ''

def _get_thread(tid):
    for t in threading.enumerate():
        if t.ident == tid:
            return t
    return None

def _pystack(sig, frame):
    pystack()

def enable_pystack():
    signal.signal(signal.SIGUSR1, _pystack)

有需要的朋友,赶紧拿走吧.

posted on 2013-08-19 13:15 stone2083 阅读(7970) 评论(0)  编辑  收藏 所属分类: python


只有注册用户登录后才能发表评论。


网站导航: