我正在尝试执行以下行
gsutil -m rsync s3://input gs://output
在蟒蛇中。在外壳终端中运行此行时,它可以正常工作。但是,我正在尝试使用以下行在 python 脚本中运行它。
subprocess.Popen(["gsutil", "-m", "rsync", "s3://input", "gs://output"])
然而,它只是永远挂起。它输出以下内容:
Building synchronization state...
Starting synchronization...
bash 命令成功打印:
Building synchronization state...
Starting synchronization...
Copying s3://input/0000
[0/1 files][ 1.0 MiB/ 5.1 MiB] (number here)% Done
并且该文件显示在我的 GS 存储桶中
我猜这是因为最后两行可能是写给stderr而不是stdout的。是否可以尝试将调用Popen
用作上下文管理器,然后调用communicate()
从输出流中读取?
proc = subprocess.Popen(["gsutil", "-m", "rsync", "s3://input", "gs://output"])
try:
outs, errs = proc.communicate(timeout=15)
# now you can do something with the text in outs and errs
except TimeoutExpired:
proc.kill()
outs, errs = proc.communicate()