2012-10-03 29 views
0

因此,我想創建一個函數,輸出重建的進度和完成此重建的時間。但一旦重建達到100%就需要結束。這是我到目前爲止有:我想在python中每5秒運行一個函數,直到變量達到100%

def progress(): 
    # This prints out the time left until rebuild is done. 
    loudEchoCMD("storage::rebuild-eta") 
    # This prints out the % of the rebuild that is done. 
    rebuildProgress = loudEchoCMD("storage::rebuild-progress") 
    print rebuildProgress 
    if rebuildProgress != '100%': 
     global t 
     t = threading.Timer(5.0, progress) 
     t.start() 
    else: 
     t.cancel() 

當我開始重建過程中,會先完成這一點,然後啓動線程而不是線程打印出來的進步和ETA每五秒鐘。

+0

哎呀對不起固定。致命的謝謝你。 – Adilicious

回答

2

當我開始重建過程中,會先完成這一點,然後 啓動線程

開始在不同的線程的重建過程。

import threading 
build = threading.Thread(target = start_rebuild) 
build.start() 
progress() 
build.join() # wait for build thread to end 

此外,假設progress時間不超過5秒就可以完成,我認爲這足以省略global tt.cancel:現在

def progress(): 
    # This prints out the time left until rebuild is done. 
    loudEchoCMD("storage::rebuild-eta") 
    # This prints out the % of the rebuild that is done. 
    rebuildProgress = loudEchoCMD("storage::rebuild-progress") 
    print rebuildProgress 
    if rebuildProgress != '100%': 
     t = threading.Timer(5.0, progress) 
     t.start() 
+0

但是,這不會做同樣的事情。 start_rebuild開始,結束,progress()開始? – Adilicious

+0

重建將在單獨的線程中發生,使主線程可用於調用進度。 – unutbu

+0

因此,這將首先啓動start_rebuild函數中的任何內容,並且在start_rebuild函數正在發生時,它會每隔五秒打印一次進度?因爲那將是完美的:D – Adilicious