fjs*_*n01 2 python subprocess gsutil google-cloud-platform
我正在尝试执行以下行
gsutil -m rsync s3://input gs://output
Run Code Online (Sandbox Code Playgroud)
在Python中。在 shell 终端中运行此行时,它工作正常。但是,我尝试使用以下行在 python 脚本中运行它。
subprocess.Popen(["gsutil", "-m", "rsync", "s3://input", "gs://output"])
Run Code Online (Sandbox Code Playgroud)
然而它只是永远挂起。它输出以下内容:
Building synchronization state...
Starting synchronization...
Run Code Online (Sandbox Code Playgroud)
bash命令成功打印:
Building synchronization state...
Starting synchronization...
Copying s3://input/0000
[0/1 files][ 1.0 MiB/ 5.1 MiB] (number here)% Done
Run Code Online (Sandbox Code Playgroud)
该文件显示在我的 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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2554 次 |
| 最近记录: |