fabric SUDO 为 运行 时是否可以执行函数

Is there possible execute function while fabric SUDO is running

我正在执行 rsync 命令并使用 stdout 实时获取输出。

我的问题是我需要在命令为 运行 时操作此输出。

我的旧代码像这样处理子进程:

cmd = 'rsync -rc --delete --progress %s %s' % (path, PATH_LOCAL_STORAGE)
with io.open("%s%s" % (TEMP_LOCAL, filename), 'wb') as writer:
        process = sudo(cmd, stdout=writer, shell=True, stdin=subprocess.PIPE)
        while process.poll() is None:
            doWhatIWant()
            time.sleep(5)

所以我的 doWhatIWant 每 5 秒执行一次,而我的代码 rsync 命令是 运行。

现在我需要使用 Fabric Sudo 而不是子进程。我已经尝试使用@Parallel 和@Task 但没有成功。

我已经使用 Threads 将其存档。

def RunMyCodeAsync(writer):
    sudo(cmd, stdout=writer, shell=True)

def DoMyCopy():
    with io.open('file.txt', 'wb') as writer:
        thread = threading.Thread(
            name='RunMyCodeAsync', 
            targed=RunMyCodeAsync, 
            args(writer,)) # args must have the comma ','
        thread.start()
        while thread.is_alive():
            DoWhatIWant()
            time.sleep(5) # Run each 5 seconds