Created
December 2, 2011 19:09
-
-
Save charles-dyfis-net/1424448 to your computer and use it in GitHub Desktop.
uWSGI thread dump trigger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# added in WSGI handler run by uUWSGI | |
import sys | |
import os.path | |
try: | |
import uwsgi | |
import threading | |
stack_dump_file = '/tmp/stack-dump-trigger' ## given as an example; put this somewhere better than /tmp | |
if not os.path.exists(stack_dump_file): | |
open(stack_dump_file, 'w') | |
def thread_dump(dummy_signum): | |
print >>sys.stderr, "### BEGIN THREAD DUMP" | |
thread_names = dict([(t.ident, t.name) for t in threading.enumerate()]) | |
for threadId, stack in sys._current_frames().items(): | |
print >>sys.stderr, "\n# ThreadID: %s (%s)" % (threadId, thread_names.get(threadId, 'NO_NAME_AVAILABLE')) | |
for filename, lineno, name, line in traceback.extract_stack(stack): | |
print >>sys.stderr, 'File: "%s", line %d, in %s' % (filename, lineno, name) | |
if line: | |
print >>sys.stderr, " %s" % (line.strip()) | |
print >>sys.stderr, "### END THREAD DUMP" | |
uwsgi.register_signal(17, 'workers', thread_dump) | |
uwsgi.add_file_monitor(17, stack_dump_file) | |
print >>sys.stderr, "Listening for stack dump requests via %r" % (stack_dump_file,) | |
except ImportError: | |
print >>sys.stderr, "Not running under uwsgi; unable to configure stack dump trigger" | |
except IOError: | |
print >>sys.stderr, "IOError creating stack dump trigger %r" % (stack_dump_file,) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment