Replace hotspot with cProfile for daemon

Added a twisted signal handler to save profiler stats when
deluged is killed with SIGINT.
This commit is contained in:
bendikro 2012-11-25 01:56:00 +01:00 committed by Calum Lind
parent 4b99a39779
commit 763f5de904

View File

@ -247,27 +247,32 @@ this should be an IP address", metavar="IFACE",
open_logfile() open_logfile()
def run_daemon(options, args):
try:
from deluge.core.daemon import Daemon
Daemon(options, args)
except deluge.error.DaemonRunningError, e:
log.error(e)
log.error("You cannot run multiple daemons with the same config directory set.")
log.error("If you believe this is an error, you can force a start by deleting %s.", deluge.configmanager.get_config_dir("deluged.pid"))
sys.exit(1)
except Exception, e:
log.exception(e)
sys.exit(1)
if options.profile: if options.profile:
import hotshot import cProfile
hsp = hotshot.Profile(deluge.configmanager.get_config_dir("deluged.profile")) profiler = cProfile.Profile()
hsp.start() profile_output = deluge.configmanager.get_config_dir("deluged.profile")
try:
from deluge.core.daemon import Daemon # Twisted catches signals to terminate
Daemon(options, args) def save_profile_stats():
except deluge.error.DaemonRunningError, e: profiler.dump_stats(profile_output)
log.error(e) print "Profile stats saved to %s" % profile_output
log.error("You cannot run multiple daemons with the same config directory set.")
log.error("If you believe this is an error, you can force a start by deleting %s.", deluge.configmanager.get_config_dir("deluged.pid")) from twisted.internet import reactor
sys.exit(1) reactor.addSystemEventTrigger("before", "shutdown", save_profile_stats)
except Exception, e: print "Running with profiler..."
log.exception(e) profiler.runcall(run_daemon, options, args)
sys.exit(1) else:
finally: run_daemon(options, args)
if options.profile:
hsp.stop()
hsp.close()
import hotshot.stats
stats = hotshot.stats.load(deluge.configmanager.get_config_dir("deluged.profile"))
stats.strip_dirs()
stats.sort_stats("time", "calls")
stats.print_stats(400)