Skip to content

Instantly share code, notes, and snippets.

@charles-dyfis-net
Created December 2, 2011 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charles-dyfis-net/1424448 to your computer and use it in GitHub Desktop.
Save charles-dyfis-net/1424448 to your computer and use it in GitHub Desktop.
uWSGI thread dump trigger
# 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