我正在执行rsync命令并实时获取输出。
我的问题是我需要在命令运行时操纵此输出。
我的旧代码与这样的子过程合作:
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命令。
现在我需要使用织物sudo而不是子进程。我已经尝试使用@parallal和@task,但没有成功。
我已经使用线程对此进行了存档。
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