diff options
author | bendikro <bendikro@gmail.com> | 2012-11-25 01:56:00 +0100 |
---|---|---|
committer | Calum Lind <calumlind+deluge@gmail.com> | 2013-01-19 02:23:01 +0000 |
commit | 763f5de904262c7611870c510de29441fa3a9c63 (patch) | |
tree | 65702d26e2ed0e323ff972e4a804733c56ef3b39 | |
parent | 4b99a39779df58a759c7bb26fca96fdc3ac2bfab (diff) | |
download | deluge-763f5de904262c7611870c510de29441fa3a9c63.tar.gz deluge-763f5de904262c7611870c510de29441fa3a9c63.tar.bz2 deluge-763f5de904262c7611870c510de29441fa3a9c63.zip |
Replace hotspot with cProfile for daemon
Added a twisted signal handler to save profiler stats when
deluged is killed with SIGINT.
-rw-r--r-- | deluge/main.py | 51 |
1 files changed, 28 insertions, 23 deletions
diff --git a/deluge/main.py b/deluge/main.py index a1525df1c..c68ab3d5e 100644 --- a/deluge/main.py +++ b/deluge/main.py @@ -247,27 +247,32 @@ this should be an IP address", metavar="IFACE", 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: - import hotshot - hsp = hotshot.Profile(deluge.configmanager.get_config_dir("deluged.profile")) - hsp.start() - 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) - finally: - 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) + import cProfile + profiler = cProfile.Profile() + profile_output = deluge.configmanager.get_config_dir("deluged.profile") + + # Twisted catches signals to terminate + def save_profile_stats(): + profiler.dump_stats(profile_output) + print "Profile stats saved to %s" % profile_output + + from twisted.internet import reactor + reactor.addSystemEventTrigger("before", "shutdown", save_profile_stats) + print "Running with profiler..." + profiler.runcall(run_daemon, options, args) + else: + run_daemon(options, args) |