[Assorted-commits] SF.net SVN: assorted: [185] python-commons/trunk/src/commons/startup.py
Brought to you by:
yangzhang
From: <yan...@us...> - 2007-11-19 07:06:24
|
Revision: 185 http://assorted.svn.sourceforge.net/assorted/?rev=185&view=rev Author: yangzhang Date: 2007-11-18 23:06:23 -0800 (Sun, 18 Nov 2007) Log Message: ----------- added sigquit thread dumper; this helped me find a bug in icedb Modified Paths: -------------- python-commons/trunk/src/commons/startup.py Modified: python-commons/trunk/src/commons/startup.py =================================================================== --- python-commons/trunk/src/commons/startup.py 2007-11-09 22:46:32 UTC (rev 184) +++ python-commons/trunk/src/commons/startup.py 2007-11-19 07:06:23 UTC (rev 185) @@ -6,10 +6,10 @@ """ from __future__ import absolute_import -from .log import ( debug, config_logging ) +from .log import ( critical, debug, config_logging ) from .environ import ( is_environ_set, get_environs ) from .interp import interp -import os, signal, sys +import os, sys class UnsetError( Exception ): pass @@ -90,8 +90,27 @@ ########################################################### -def run_main( main = None, do_force = False, runner = None ): +from signal import * + +def dump_stack(*args): """ + Useful for debugging your program if it's spinning into infinite loops. + Install this signal handler and issue your program a SIGQUIT to dump the + main thread's stack, so you can see where it is. + """ + from traceback import print_stack + from cStringIO import StringIO + s = StringIO() + print_stack(file = s) + print s.getvalue() + sys.stdout.flush() + print >> sys.stderr, s.getvalue() + sys.stderr.flush() + critical( '', s.getvalue() ) + +def run_main( main = None, do_force = False, runner = None, + use_sigquit_handler = False ): + """ A feature-ful program starter. Configures logging and psyco, then runs the C{main} function defined in the caller's module, passing in L{sys.argv}. If the C{PYDBG} environment variable is set, then @@ -105,6 +124,9 @@ print "Hello " + argv[1] run_main() """ + + if use_sigquit_handler: signal( SIGQUIT, dump_stack ) + # TODO figure out a better solution for this config_logging() config_psyco() @@ -124,7 +146,7 @@ if do_debug: import pdb - signal.signal(signal.SIGINT, lambda *args: pdb.set_trace()) + signal(SIGINT, lambda *args: pdb.set_trace()) status = pdb.runcall( runner, main, sys.argv ) elif do_profile: import cProfile as profile This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |