Thread: [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. |
From: <yan...@us...> - 2008-01-17 23:15:13
|
Revision: 233 http://assorted.svn.sourceforge.net/assorted/?rev=233&view=rev Author: yangzhang Date: 2008-01-17 15:15:04 -0800 (Thu, 17 Jan 2008) Log Message: ----------- added multi-threaded stack dumps to sigquit handler Modified Paths: -------------- python-commons/trunk/src/commons/startup.py Modified: python-commons/trunk/src/commons/startup.py =================================================================== --- python-commons/trunk/src/commons/startup.py 2008-01-17 23:13:45 UTC (rev 232) +++ python-commons/trunk/src/commons/startup.py 2008-01-17 23:15:04 UTC (rev 233) @@ -9,6 +9,7 @@ from .log import ( critical, debug, config_logging ) from .environ import ( is_environ_set, get_environs ) from .interp import interp +from sys import _current_frames import os, sys class UnsetError( Exception ): pass @@ -101,12 +102,15 @@ from traceback import print_stack from cStringIO import StringIO s = StringIO() - print_stack(file = s) - print s.getvalue() + for tid, frame in _current_frames().iteritems(): + print >> s, 'thread', tid + print_stack(f = frame, file = s) + output = s.getvalue() + print output sys.stdout.flush() - print >> sys.stderr, s.getvalue() + print >> sys.stderr, output sys.stderr.flush() - critical( '', s.getvalue() ) + critical( '', output ) def run_main( main = None, do_force = False, runner = None, use_sigquit_handler = False ): This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <yan...@us...> - 2008-01-20 06:25:26
|
Revision: 248 http://assorted.svn.sourceforge.net/assorted/?rev=248&view=rev Author: yangzhang Date: 2008-01-19 22:25:30 -0800 (Sat, 19 Jan 2008) Log Message: ----------- added better profiling facility, made consistent with multithreaded facility; removed is_environ_set Modified Paths: -------------- python-commons/trunk/src/commons/startup.py Modified: python-commons/trunk/src/commons/startup.py =================================================================== --- python-commons/trunk/src/commons/startup.py 2008-01-20 00:27:16 UTC (rev 247) +++ python-commons/trunk/src/commons/startup.py 2008-01-20 06:25:30 UTC (rev 248) @@ -7,9 +7,10 @@ from __future__ import absolute_import from .log import ( critical, debug, config_logging ) -from .environ import ( is_environ_set, get_environs ) +from .environ import ( get_environs ) from .interp import interp from sys import _current_frames +from threading import currentThread import os, sys class UnsetError( Exception ): pass @@ -145,21 +146,26 @@ runner = ( ( lambda main, args: main( args ) ) if runner is None else runner ) main = frame.f_globals[ 'main' ] if main is None else main - do_debug = is_environ_set( 'PYDBG' ) - do_profile = is_environ_set( 'PYPROF' ) + do_debug = os.environ.get( 'PYDBG', '' ) != '' + do_profile = os.environ.get( 'PYPROF', '' ) != '' if do_debug: import pdb signal(SIGINT, lambda *args: pdb.set_trace()) status = pdb.runcall( runner, main, sys.argv ) elif do_profile: - import cProfile as profile - container = {} - output_path = os.environ[ 'PYPROF' ] - profile.runctx( - 'container[ "status" ] = runner( main, sys.argv )', - globals(), locals(), filename = output_path ) - status = container[ 'status' ] + from cProfile import runctx + container = [] + try: + outpath = os.environ[ 'PYPROF' ] % \ + currentThread().getName() + except: + error( 'bad PYPROF:', os.environ[ 'PYPROF' ] ) + status = runner( main, sys.argv ) + else: + runctx( 'container[0] = runner( main, sys.argv )', + globals(), locals(), filename = outpath ) + status = container[0] else: status = runner( main, sys.argv ) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <yan...@us...> - 2008-07-13 19:20:52
|
Revision: 888 http://assorted.svn.sourceforge.net/assorted/?rev=888&view=rev Author: yangzhang Date: 2008-07-13 12:21:00 -0700 (Sun, 13 Jul 2008) Log Message: ----------- added command_name(); added handle_exceptions to startup() Modified Paths: -------------- python-commons/trunk/src/commons/startup.py Modified: python-commons/trunk/src/commons/startup.py =================================================================== --- python-commons/trunk/src/commons/startup.py 2008-07-13 18:47:55 UTC (rev 887) +++ python-commons/trunk/src/commons/startup.py 2008-07-13 19:21:00 UTC (rev 888) @@ -9,6 +9,7 @@ from .log import ( critical, debug, config_logging ) from .environ import ( get_environs ) from .interp import interp +from os.path import basename from sys import _current_frames from threading import currentThread import os, sys @@ -113,8 +114,11 @@ sys.stderr.flush() critical( '', output ) +def command_name(): + return basename(sys.argv[0]) + def run_main( main = None, do_force = False, runner = None, - use_sigquit_handler = False ): + use_sigquit_handler = False, handle_exceptions = False ): """ A feature-ful program starter. Configures logging and psyco, then runs the C{main} function defined in the caller's module, passing @@ -149,25 +153,32 @@ do_debug = os.environ.get( 'PYDBG', '' ) != '' do_profile = os.environ.get( 'PYPROF', '' ) != '' - if do_debug: - import pdb - signal(SIGINT, lambda *args: pdb.set_trace()) - status = pdb.runcall( runner, main, sys.argv ) - elif do_profile: - from cProfile import runctx - container = [] - try: - outpath = os.environ[ 'PYPROF' ] % \ - currentThread().getName() - except: - error( 'bad PYPROF:', os.environ[ 'PYPROF' ] ) + try: + if do_debug: + import pdb + signal(SIGINT, lambda *args: pdb.set_trace()) + status = pdb.runcall( runner, main, sys.argv ) + elif do_profile: + from cProfile import runctx + container = [] + try: + outpath = os.environ[ 'PYPROF' ] % \ + currentThread().getName() + except: + error( 'bad PYPROF:', os.environ[ 'PYPROF' ] ) + status = runner( main, sys.argv ) + else: + runctx( 'container[0] = runner( main, sys.argv )', + globals(), locals(), filename = outpath ) + status = container[0] + else: status = runner( main, sys.argv ) + except BaseException, ex: + if handle_exceptions: + print >> sys.stderr, ex.message + sys.exit(1) else: - runctx( 'container[0] = runner( main, sys.argv )', - globals(), locals(), filename = outpath ) - status = container[0] - else: - status = runner( main, sys.argv ) + raise ## watchdog timer commits suicide if after 3 seconds we ## still have not exited This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |