0

所以我想创建一个函数来打印重建的进度和完成重建的剩余时间。但是一旦重建达到 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。

4

1 回答 1

2

当我开始重建过程时,它将首先完成然后开始线程

在不同的线程中启动重建过程。

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

另外,假设progress完成时间不到 5 秒,我认为省略global tand就足够了t.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()
于 2012-10-03T12:03:34.723 回答