2011-11-11 24 views
0

我有一些應用程序啓動時間很慢。配置文件GUI應用程序引導

理論上我只是想在gui啓動後立即退出。直到現在我已經完成了它的手工,它的工作,但我不知道是否有更好的方法...

一個解決方案當然會添加一個「sys.exit」,我想要它退出,但我應該修改代碼。

有沒有辦法讓文件在一些條件下退出而不修改它?

+0

你是想說你的應用程序需要很長的時間來啓動,並要找出原因,這樣你就可以修復它,使其更快啓動?如果是這樣,你可能想* [試試這個](http://stackoverflow.com/questions/4295799/how-to-improve-performance-of-this-code/4299378#4299378)*。 –

+0

你確定這是正確的鏈接? 無論如何是的,該應用需要很長時間,而且我知道如何進行配置,但我不知道如何配置自舉。 我基本上需要在GUI啓動後立即退出,而不需要手動執行它... –

+0

難道你不能在應用程序啓動時按Ctrl-C嗎?然後python將停止,並顯示堆棧。那會告訴你到底它在做什麼。由於它變得如此難以忍受,所以你不會抓住這個浪費的東西的可能性非常小。可以肯定的是,做幾次。這與分析不同。你可能會認爲這是事實,但事實並非如此。它比探查器要快得多。 –

回答

0

好吧,那麼確切地設置一個「出口點」就是我想要的位置而不需要觸摸代碼並不是那麼簡單。

然而,可以做的,我認爲,隨着sys.settrace打轉轉,如下所示 http://www.doughellmann.com/PyMOTW/sys/tracing.html

所以對我來說,解決辦法是實際修改代碼,只需添加一些退出線點。

我想用difflib,但它並沒有打補丁的功能,所以我創建了下面的小腳本,它在短: - 讀取文件 - 插入/刪除一條線(上插入具有相同縮進作爲前行) - 改寫

#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)