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

标签: pystack python stack | 发表时间:2013-08-19 13:15 | 作者:stone2083
出处:http://www.blogjava.net/
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)

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

stone2083 2013-08-19 13:15 发表评论

相关 [pystack python stack] 推荐:

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

- - BlogJava-首页技术区
类似java中的jstack功能. https://pypi.python.org/pypi/pdbx/0.3.0 下载, 或者直接通过easyinstall安装. python scripts中, import pdbx; pdbx.enable_pystack(); 开启pystack功能. kill -30 pid , 就可以打印stack信息了. .

Stack Exchange 的架构

- Krevy - 酷壳 - CoolShell.cn
近日,Stack Exchange系统管理员blog上发布了一篇关于Stack Exchange的架构一瞥,其包括了Stack Overflow, Server Fault 和 Super User的 Stack Exchange 网络. 1 机柜 位于俄勒冈的 Peak Internet (用于chat 和Data Explorer).

Stack Overflow 的現況…

- - Gea-Suan Lin's BLOG
Stack Overflow 的 Nick Craver 貼出目前 Stack Overflow 的現況:「 What it takes to run Stack Overflow」. 公開出來的資料不包括 CDN 的部份,可以看出整個架構很精簡啊… 然後還貼出機房照片:. 可以看出很多機器都很大台,尤其是 RAM 的部份.

说说Stack Overflow和Quora

- corleone1969 - robbin的自言自语
今天看到一个新闻,Quora的中国克隆“知乎”得到了创新工场的投资. 我之前从创新工场的投资经理张亮那里要到了一个知乎邀请码,最近一直泡知乎,觉得Quora类的产品有很多创新的亮点,所以比较感兴趣这类产品,忍不住就谈谈. Stack Overflow(以下简称SO)和Quora虽然都是知识问答类的网站,但是他们有共同的成功基因,也有本质的差别.

Stack的三种含义

- - 阮一峰的网络日志
学习编程的时候,经常会看到stack这个词,它的中文名字叫做"栈". 理解这个概念,对于理解程序的运行至关重要. 容易混淆的是,这个词其实有三种含义,适用于不同的场合,必须加以区分. stack的第一种含义是一组数据的 存放方式,特点为LIFO,即后进先出(Last in, first out).

java - C3P0: unreturnedConnectionTimeout in production? - Stack Overflow

- -
unreturnedConnectionTimeoutin production mostly if for some reason you can't debug and fix the application whose Connections are leaking, in which case it's a reasonable workaround to just set.

How to configure autoscaling on docker swarm? - Stack Overflow

- -
This will involve a lot of scripting but the idea is to monitor the cluster for CPU / Memory / Network usage (with.

为什么 Stack Overflow 会如此成功?

- XXL - DBA Notes
说起问答(Question & Answer)网站,很多人会第一反应想起 Quora ,实际上,这类网站中 Quora 并非做的最好的(但或许是借了 Facebook 的风头),最为成功的要数 Stack Overflow. 更为准确的说,是 Stack Exchange Network,Stack Overflow 现在只是 Stack Exchange network 的子站点而已.

Git branching strategy integated with testing/QA process - Stack Overflow

- -
In case a feature would not be accepted after testing but we would like to release other features already merged on develop that would be hell. This is a tricky step, I think the best way to avoid it is to keep features as small/specific as possible.