【问题标题】:Python getting output from running shell command - gcloud create dataproc clusterPython 从运行 shell 命令获取输出 - gcloud create dataproc cluster
【发布时间】:2022-01-16 18:13:17
【问题描述】:

我尝试使用 python

从运行 gcloud-dataproc-create-cluster 中获取 expire-dataproc-tag

我尝试了 subprocess.Popen,我认为由于它是一个错误或检索结果需要很长时间,我最终得到一个空字符串

我试过command,command_1工作正常,运行command_2时出现问题

import subprocess

command = "echo hello world"
command_1 = "gcloud compute images list --project {project-id} --no-standard-images"
command_2 = 'gcloud beta dataproc clusters create cluster-name --bucket {bucket} --region europe-west1 --zone europe-west1-b --subnet {subnet} --tags {tag} --project {project-id} --service-account {service-account} --master-machine-type n1-standard-16 --master-boot-disk-size 100 --worker-machine-type n1-standard-1 --worker-boot-disk-size 100 --image {image} --max-idle 2h --metadata enable-oslogin=true --properties {properties} --optional-components=ANACONDA,JUPYTER,ZEPPELIN --enable-component-gateway --single-node --no-address'.split(' ')

process = subprocess.Popen(command_2, stdout=subprocess.PIPE, shell=True)
# process.wait()
try:
    print('inside-try')
    result, err = process.communicate()
    result = result.decode('utf-8')
except Exception as e:
    print('The Error', e)

print('the result: ', result)
print("the-error: ", err)

输出是

inside-try  
ERROR: (gcloud.beta.dataproc.clusters.create) INVALID_ARGUMENT: Dataproc custom image '{image-name}' has expired. Please rebuild this custom image. To extend the custom image expiration date to '2022-02-11T08:29:58.322549Z', please use this cluster property during cluster creation: 'dataproc:dataproc.custom.image.expiration.token=1.{image-name-properties......}'               
the result: 
the-error: None

我正在尝试获取 ERROR: .... 输出到结果变量(在结果之后打印)

【问题讨论】:

    标签: python shell google-cloud-platform gcloud


    【解决方案1】:

    您没有从进程中捕获stderr

    试试:

    process = subprocess.Popen(
        command,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        shell=True
    )
    

    所以err 不是由result, err = process.communicate() 设置的

    通过上述更改,err 将包含您收到的错误消息。

    我强烈建议您考虑使用 Google 的 SDK 与其服务进行交互。这些不仅更易于使用,而且您可以发送 Python 对象,而不是将字符串输入/输出子流程。

    这是 Python 中Creating a Dataproc cluster 的文档。

    【讨论】:

    • 非常感谢您的支持,非常感谢
    • 不客气!
    猜你喜欢
    • 1970-01-01
    • 2020-03-25
    • 1970-01-01
    • 2013-08-21
    • 2021-07-26
    • 2018-07-24
    • 2018-07-30
    • 2011-05-21
    • 2018-02-11
    相关资源
    最近更新 更多