0

I have some applications which suffer of very slow boot time.

In theory I just want to exit as soon as the gui fires up. Until now I've done it by hand, and it worked, but I wonder if there is a better way...

A solution of course would be to add a "sys.exit" where I want to make it quit, but I should modify the code then.

Is there a way to instrument the file to quit on some conditions without modifying it?

andrea_crotti
  • 3,004
  • 2
  • 28
  • 33
  • Are you trying to say your app takes a long time to start, and you want to find out why, so you can fix it to make it start up faster? If so, you might want to *[try this](http://stackoverflow.com/questions/4295799/how-to-improve-performance-of-this-code/4299378#4299378)*. – Mike Dunlavey Nov 11 '11 at 14:56
  • Are you sure it's the right link? Anyway yes, the app takes a long time, and I do know how to profile it, but what I don't know is how to profile only the bootstrap. I basically need to make it quit as soon as the GUI fires up, without doing it manually... – andrea_crotti Nov 11 '11 at 15:12
  • Can't you just hit Ctrl-C while your app is starting? Then python will stop, and display the stack. That will tell you exactly what it's doing at that time. Since it is taking so unbearably long, the chance that you won't catch it doing the wasteful thing is very small. To be sure, do it a few times. This is not the same as profiling. You might think it is, but it's not. It takes you straight to the problem far more quickly than a profiler does. – Mike Dunlavey Nov 11 '11 at 19:57
  • Ehm no it's not what I want... I want to measure exactly the time spent in the first phase, also to see if the changes that I do are actually useful. – andrea_crotti Nov 11 '11 at 20:28
  • Well some sort of timer would do that. What the random-pausing does is tell you what you should fix, as in *[this example](http://stackoverflow.com/questions/926266/performance-optimization-strategies-of-last-resort/927773#927773)* of a speedup factor of 43. If you'd just rather try fixing things, without knowing for certain if they're the right things to fix, you can do that. – Mike Dunlavey Nov 11 '11 at 21:41

1 Answers1

0

Alright then, to actually set an "exit point" exactly where I want without touching the code is not so trivial.

It can be done however, I think, playing around with sys.settrace, as shown here http://www.doughellmann.com/PyMOTW/sys/tracing.html

So the solution for me is to actually modify the code, simply adding an exit line at some point.

I wanted to use the difflib but it doesn't have patching functions, so I created the small script below, which in short: - reads the file - insert / deletes one line (on insertion with the same indentation as the previous line) - rewrite it

#TODO: must make sure about the indentation
import argparse
import re
import sys

PATCH_LINE = "import sys; sys.exit(0)  # PATCHED"


def parse_arguments():
    # take the file and the line to patch, or maybe we can take a
    # diff file generated via uniform_diff
    parser = argparse.ArgumentParser(description='enable and disable the automatic exit')
    parser.add_argument('file', help='file to patch')

    parser.add_argument('line', help='line where to quit')

    parser.add_argument('-m', '--msg',
                        default=PATCH_LINE)

    parser.add_argument('-d', '--disable',
                        action='store_true')

    return parser.parse_args()


if __name__ == '__main__':
    ns = parse_arguments()
    text = open(ns.file).readlines()
    line_no = int(ns.line)

    if ns.disable:
        # the line should not be necessary in that case?
        text.remove(text[line_no])
    else:
        # count spaces
        prev = text[line_no - 1]
        m = re.match('\s*', prev)
        to_insert = m.group() + ns.msg
        print("inserting line %s" % to_insert)
        text.insert(line_no, to_insert)

    open(ns.file, 'w').writelines(text)
andrea_crotti
  • 3,004
  • 2
  • 28
  • 33