【问题标题】:Python subprocess not working in Google Cloud FunctionsPython 子进程在 Google Cloud Functions 中不起作用
【发布时间】:2020-02-20 16:07:03
【问题描述】:

我需要在 Google Cloud Function 中通过 Python 中的 Subprocess 执行一些进程。

import subprocess
import os
def hello_world(request):
    print(subprocess.call(["echo", "hello","world"]))

预期输出: 你好世界

实际输出: 0

谷歌函数会阻止子进程的执行还是我需要以不同的方式接收输出

【问题讨论】:

    标签: python google-cloud-platform google-cloud-functions subprocess


    【解决方案1】:

    可以使用subprocess。如果要返回子进程调用的输出,而不是退出代码,则必须使用subprocess.check_output()(并返回结果):

    import subprocess
    
    def hello_world(request):
        return subprocess.check_output(["echo", "'hello world'"])
    

    但是,如果您只是想返回一个字符串,这是不必要的,以下就足够了:

    def hello_world(request):
        return "hello world"
    

    【讨论】:

      猜你喜欢
      • 2017-06-27
      • 1970-01-01
      • 2019-11-15
      • 2020-03-05
      • 2013-10-15
      • 2019-07-09
      • 2020-04-08
      • 2020-03-31
      • 2021-12-26
      相关资源
      最近更新 更多