使用信号显示Python进程的堆栈跟踪
项目描述
使用信号显示Python进程的堆栈跟踪和垃圾回收统计,如 frsyuki/sigdump。
用法
以下是一个启用sigdump的服务器示例
import os
from wsgiref.simple_server import make_server
import sigdump
def application(env, start_response):
start_response('200 OK', [('Content-type', 'text/plain; charset=utf-8')])
return [b'Hello World']
if __name__ == '__main__':
sigdump.enable(verbose=True) # just calling sigdump.signal()
print("pid:", os.getpid())
httpd = make_server('', 8000, application)
httpd.serve_forever()
$ python example/wsgi.py
pid: 57650
SIGDUMP is enabled. The result is exported to /tmp/sigdump-82323.log.
然后发送一个 SIGCONT 信号。如果您想更改信号,请设置 SIGDUMP_SIGNAL 环境变量。
$ kill -s SIGCONT 57650
请查看 /tmp/sigdump-<pid>.log。如果您想更改输出路径,请设置 SIGDUMP_PATH 环境变量。您可以将 "-" 设置为将输出到 STDOUT,或 "+" 设置为 STDERR。
$ less /tmp/sigdump-57650.log
Sigdump at 2019-12-06 21:04:55.071633 process 57650
Stacktrace:
File "example/wsgi.py", line 15, in <module>
httpd.serve_forever()
File "/Users/c-bata/.pyenv/versions/3.7.1/lib/python3.7/socketserver.py", line 232, in serve_forever
ready = selector.select(poll_interval)
File "/Users/c-bata/.pyenv/versions/3.7.1/lib/python3.7/selectors.py", line 415, in select
fd_event_list = self._selector.poll(timeout)
GC stat:
Generation 0:
collections : 33
collected : 99
uncollectable : 0
Generation 1:
collections : 2
collected : 253
uncollectable : 0
Generation 2:
collections : 0
collected : 0
uncollectable : 0
许可证
MIT License Copyright (c) 2016 Masashi SHIBATA Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.